字符串操作 VB.net

发布于 2024-11-16 19:51:18 字数 1191 浏览 3 评论 0原文

下面是输入文件

     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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2024-11-23 19:51:18

编辑后包括保存到文件和屏幕上。

您希望将最终输出打印到屏幕上,还是保存到另一个文本文件中?
这是它在屏幕上显示的方式以及保存到 OUTPUT.TXT

 Dim tmpLine as String
 Dim FirstLine as Boolean = True

    Dim fh As StreamReader
    Dim fout as StreamWriter
    fh = New StreamReader("list.txt")
    fout = New StreamWriter("output.txt")

    Dim line As String = fh.ReadLine()
    Dim lineData As String() = Nothing

    While Not line Is Nothing
        lineData = line.Split(" ")

       If FirstLine=False Then
            If lineData(1).Equals("USD") Then
                 Console.WriteLine(tmpLine & " Yes")
                 fout.WriteLine(tmpLine & " Yes")
            Else
                 Console.WriteLine(tmpLine & " No") 
                 fout.WriteLine(tmpLine & " No")
            End If
        Else
             FirstLine = False
        End If

        tmpLine = line
        line = fh.ReadLine
    End While
    fh.Close()

        If lineData(1).Equals("USD") Then
             Console.WriteLine(tmpLine & " Yes")
             fout.WriteLine(tmpLine & " Yes")
        Else
             Console.WriteLine(tmpLine & " No") 
             fout.WriteLine(tmpLine & " No")
        End If
    fout.Close()

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

 Dim tmpLine as String
 Dim FirstLine as Boolean = True

    Dim fh As StreamReader
    Dim fout as StreamWriter
    fh = New StreamReader("list.txt")
    fout = New StreamWriter("output.txt")

    Dim line As String = fh.ReadLine()
    Dim lineData As String() = Nothing

    While Not line Is Nothing
        lineData = line.Split(" ")

       If FirstLine=False Then
            If lineData(1).Equals("USD") Then
                 Console.WriteLine(tmpLine & " Yes")
                 fout.WriteLine(tmpLine & " Yes")
            Else
                 Console.WriteLine(tmpLine & " No") 
                 fout.WriteLine(tmpLine & " No")
            End If
        Else
             FirstLine = False
        End If

        tmpLine = line
        line = fh.ReadLine
    End While
    fh.Close()

        If lineData(1).Equals("USD") Then
             Console.WriteLine(tmpLine & " Yes")
             fout.WriteLine(tmpLine & " Yes")
        Else
             Console.WriteLine(tmpLine & " No") 
             fout.WriteLine(tmpLine & " No")
        End If
    fout.Close()
渔村楼浪 2024-11-23 19:51:18

具有明确定义的输入格式并使用 split()。

输入:

DELL NOTEBOOK
1000 USD
ACER NOTEBOOK
HP NOTEBOOK
APPLE MOBILE
900 USD
HTC MOBILE
800 USD

方法:

    Dim fh As StreamReader
    fh = New StreamReader("list.txt")
    Dim line As String = fh.ReadLine()
    Dim nextLine As String = fh.ReadLine()

    While line IsNot Nothing
        If nextLine IsNot Nothing Then
            Dim lineData As String() = nextLine.Split(" ")
            If lineData(1).Equals("USD") Then
                Console.WriteLine(line & " Yes")
                Console.WriteLine(nextLine)
            Else
                Console.WriteLine(line & " No")
                Console.WriteLine(nextLine & " No")
            End If
            line = fh.ReadLine
            nextLine = fh.ReadLine
        Else
            Console.WriteLine(line & " No")
            line = fh.ReadLine
        End If
    End While
    fh.Close()
    End Sub

Have a well defined input format and use split().

Input:

DELL NOTEBOOK
1000 USD
ACER NOTEBOOK
HP NOTEBOOK
APPLE MOBILE
900 USD
HTC MOBILE
800 USD

Method:

    Dim fh As StreamReader
    fh = New StreamReader("list.txt")
    Dim line As String = fh.ReadLine()
    Dim nextLine As String = fh.ReadLine()

    While line IsNot Nothing
        If nextLine IsNot Nothing Then
            Dim lineData As String() = nextLine.Split(" ")
            If lineData(1).Equals("USD") Then
                Console.WriteLine(line & " Yes")
                Console.WriteLine(nextLine)
            Else
                Console.WriteLine(line & " No")
                Console.WriteLine(nextLine & " No")
            End If
            line = fh.ReadLine
            nextLine = fh.ReadLine
        Else
            Console.WriteLine(line & " No")
            line = fh.ReadLine
        End If
    End While
    fh.Close()
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文