字符串操作 VB.net
下面是输入文件
DELL NOTEBOOK
1000 USD
ACER NOTEBOOK
HP NOTEBOOK
APPLE MOBILE
900 USD
HTC MOBILE
800 USD
基本上我需要检查第二行是否有任何单词“USD”并输入 第一行是或否。 下面的预期输出
DELL NOTEBOOK YES
1000 USD
ACER NOTEBOOK NO
HP NOTEBOOK NO
APPLE MOBILE YES
900 USD
HTC MOBILE YES
800 USD
是我的代码,需要一些调整
Sub Main()
Dim fh As StreamReader
fh = new StreamReader("list.txt")
dim currency as string
dim bCurrency as boolean
Dim s As String = fh.ReadLine()
While not s Is Nothing
currency = s.substring(5,3)
if currency = "USD" then
bCurrency = True
else
if bCurrency = true then
Console.WriteLine(s & " Yes")
bCurrency = False
else
Console.WriteLine(s & " No")
end if
end if
s = fh.ReadLine
End While
fh.Close()
End Sub
Below is the input file
DELL NOTEBOOK
1000 USD
ACER NOTEBOOK
HP NOTEBOOK
APPLE MOBILE
900 USD
HTC MOBILE
800 USD
Basically I need to check if there any word "USD" on the second line and put
the word Yes or No. on the first line.
Expected Output
DELL NOTEBOOK YES
1000 USD
ACER NOTEBOOK NO
HP NOTEBOOK NO
APPLE MOBILE YES
900 USD
HTC MOBILE YES
800 USD
below is my code that need some tweak
Sub Main()
Dim fh As StreamReader
fh = new StreamReader("list.txt")
dim currency as string
dim bCurrency as boolean
Dim s As String = fh.ReadLine()
While not s Is Nothing
currency = s.substring(5,3)
if currency = "USD" then
bCurrency = True
else
if bCurrency = true then
Console.WriteLine(s & " Yes")
bCurrency = False
else
Console.WriteLine(s & " No")
end if
end if
s = fh.ReadLine
End While
fh.Close()
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑后包括保存到文件和屏幕上。
您希望将最终输出打印到屏幕上,还是保存到另一个文本文件中?
这是它在屏幕上显示的方式
以及保存到 OUTPUT.TXT
Edited to include Saving to file and on screen both.
Do you want the final output to be printed to the screen, or saved into another text file?
Here is the way it would appear on screen
AS WELL AS SAVED to OUTPUT.TXT
具有明确定义的输入格式并使用 split()。
输入:
方法:
Have a well defined input format and use split().
Input:
Method: