带子字符串的VB.net拆分函数

带子字符串的VB.net拆分函数,vb.net,Vb.net,我想读取字符串中的某个值。每行都是一个新字符串,我想读取每行的第6个整数 Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles browsebtn.Click If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Dim filename As String = OpenFi

我想读取字符串中的某个值。每行都是一个新字符串,我想读取每行的第6个整数

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles browsebtn.Click
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim filename As String = OpenFileDialog1.FileName
        Dim streamreader As New System.IO.StreamReader(filename)
        Dim textfile As String = streamreader.ReadToEnd
        Dim splitChar As String = vbNewLine
        Dim day As Integer = textfile.Substring(10, 2)
        Dim strLine() As String = day.Split(splitChar)
        For Each line As String In strLine

            MsgBox(day)

        Next


    End If
End Sub
End Class
但它只返回一个数字。如果我将day设置为字符串而不是整数,那么它工作得非常完美,只是它读取整个字符串,而不是我需要的两个整数。请帮忙。我做错了什么

编辑:

输入文件如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing
我希望我的输出是:

10
10

10来自“121010”

您编写的所有代码都可以用更少的行完成,如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing
不过,就像您的示例一样,它一次将整个文件加载到内存中,如果它是一个大文件,则可能会出现问题。如果需要考虑文件的大小,最好一次只读取一行,如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing
但是,问题仍然存在,如何将该行拆分为各个值。如果如您的问题中所示,值之间用空格分隔,则可以使用
line.Split
将该行分隔为其所有值的数组。然后,要获取其中一个值的最后两个字符,只需使用
String.SubString
,如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing

您编写的所有代码都可以用更少的行完成,如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing
不过,就像您的示例一样,它一次将整个文件加载到内存中,如果它是一个大文件,则可能会出现问题。如果需要考虑文件的大小,最好一次只读取一行,如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing
但是,问题仍然存在,如何将该行拆分为各个值。如果如您的问题中所示,值之间用空格分隔,则可以使用
line.Split
将该行分隔为其所有值的数组。然后,要获取其中一个值的最后两个字符,只需使用
String.SubString
,如下所示:

23728 121010 00004986 00 00  2 21 22 11   447 114  2   382   292   350

23730 121010 00064120 00 00 51 19 21 12  1064 110  2  4500   572  7734 
For Each line As String In File.ReadAllLines(fileName)
    MessageBox.Show(line)
Next
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    MessageBox.Show(line)
Loop Until line Is Nothing
Dim streamReader As New StreamReader(fileName)
Dim line As String = Nothing
Do
    line = streamReader.ReadLine()
    Dim parts() As String = line.Split()
    Dim wholeNumber As String = parts(1)
    Dim lastTwo As String = wholeNumber.SubString(wholeNumber.Length - 2)
    MessageBox.Show(lastTwo)
Loop Until line Is Nothing
一些建议:

  • 始终处置资源(使用或尝试使用 资源中心(关闭)

  • 永远不要相信用户的输入

  • 编写能够处理足够多不希望出现的情况的代码

  • 基于代码的更正:

     Try
            Dim text As String = Nothing
    
            Using streamreader As New System.IO.StreamReader("text.txt")
                text = streamreader.ReadToEnd()
            End Using
            If IsNothing(text) = False Then
                Dim strLine() As String = text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
                For Each line As String In strLine
    
                    If line.Length > 12 Then MsgBox(line.Substring(10, 2))
                Next
            End If
        Catch ex As Exception
            'filenotfound case
        End Try
    
    另一种方式:

    在行输入可以不同的情况下(但在我们的情况下,第二个值应该是第二个值)

    然后可以使用
    Regex

    以下是如何:

    Try
        Using streamreader As New System.IO.StreamReader(file)
            Dim line As String
            While streamreader.Peek > 0
                'unreaded line from file
                line = streamreader.ReadLine()
                'split input by non digits
                Dim numberstrs As String() = Regex.Split(line, "\D+")
                'second numbers last two
                If numberstrs.Length > 1 AndAlso numberstrs(1).Length > 2 Then
                    Console.WriteLine("{0}", numberstrs(1).Substring(numberstrs(1).Length - 2, 2))
                End If
    
            End While
    
    
        End Using
    Catch ex As Exception
    
    End Try
    
    一些建议:

  • 始终处置资源(使用或尝试使用 资源中心(关闭)

  • 永远不要相信用户的输入

  • 编写能够处理足够多不希望出现的情况的代码

  • 基于代码的更正:

     Try
            Dim text As String = Nothing
    
            Using streamreader As New System.IO.StreamReader("text.txt")
                text = streamreader.ReadToEnd()
            End Using
            If IsNothing(text) = False Then
                Dim strLine() As String = text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
                For Each line As String In strLine
    
                    If line.Length > 12 Then MsgBox(line.Substring(10, 2))
                Next
            End If
        Catch ex As Exception
            'filenotfound case
        End Try
    
    另一种方式:

    在行输入可以不同的情况下(但在我们的情况下,第二个值应该是第二个值)

    然后可以使用
    Regex

    以下是如何:

    Try
        Using streamreader As New System.IO.StreamReader(file)
            Dim line As String
            While streamreader.Peek > 0
                'unreaded line from file
                line = streamreader.ReadLine()
                'split input by non digits
                Dim numberstrs As String() = Regex.Split(line, "\D+")
                'second numbers last two
                If numberstrs.Length > 1 AndAlso numberstrs(1).Length > 2 Then
                    Console.WriteLine("{0}", numberstrs(1).Substring(numberstrs(1).Length - 2, 2))
                End If
    
            End While
    
    
        End Using
    Catch ex As Exception
    
    End Try
    

    史蒂文的回答让你了解了大部分情况,但不是全部。值得注意的是,您实际上不想要第6个整数,因为它可能是1或2,或者几乎任何东西,这取决于您如何分割它。同样,在你的例子中,你说你想从121010中得到10,这可能是字符串中第二组两个数字或第三组两个数字

    我注意到,在您的示例字符串中有一些双空格:您需要对其进行排序以作为开始,否则使用
    String.Split
    将为您提供数组中的空元素。事实上,使用上面Steven使用的
    parts(5)
    可以得到一个空元素,这要归功于双空格,而这不是您想要的。您需要
    部分(2)
    ,然后需要
    子字符串
    来获得所需的数字

    另一种更优雅的方法是使用正则表达式来获取数字。假设您想要该字符串中的第二个10(以粗体显示):12*10*10。如果您知道该字符串将始终为6个字符,将始终是输入行中的第二个字段,并且您始终需要第三个和第四个数字,那么您可以:

    Imports System.Text.RegularExpressions
    Imports System.IO
    
    Private Sub ReadFile
        Dim rx As New Regex("^[^\s]+\s{1}\d{2}(\d{2}?)", RegexOptions.Compiled Or RegexOptions.CultureInvariant)
        Dim streamReader As New StreamReader(fileName)
        Dim line As String = String.Empty
    
        Do While streamReader.Peek >= 0
            line = streamReader.ReadLine()
            MessageBox.Show(rx.Matches(line)(0).Groups(1).Value)
        Loop 
    End Sub
    

    我并不是说这是唯一(或最优雅)的正则表达式,但它会起作用,意味着您不必使用子字符串,也不在乎第一个字段的长度。它还假定字段之间只有一个空格,但也可以根据需要进行更改。只要你能制定出一个规则来达到你想要的位置,你就可以对它进行正则化。使用Expresso(免费且功能强大的实用程序)帮助您构建合适的表达式

    史蒂文的回答让你了解了大部分,但不是全部。值得注意的是,您实际上不想要第6个整数,因为它可能是1或2,或者几乎任何东西,这取决于您如何分割它。同样,在你的例子中,你说你想从121010中得到10,这可能是字符串中第二组两个数字或第三组两个数字

    我注意到,在您的示例字符串中有一些双空格:您需要对其进行排序以作为开始,否则使用
    String.Split
    将为您提供数组中的空元素。事实上,使用上面Steven使用的
    parts(5)
    可以得到一个空元素,这要归功于双空格,而这不是您想要的。您需要
    部分(2)
    ,然后需要
    子字符串
    来获得所需的数字

    另一种更优雅的方法是使用正则表达式来获取数字。假设您想要该字符串中的第二个10(以粗体显示):12*10*10。如果您知道该字符串将始终为6个字符,将始终是输入行中的第二个字段,并且您始终需要第三个和第四个数字,那么您可以:

    Imports System.Text.RegularExpressions
    Imports System.IO
    
    Private Sub ReadFile
        Dim rx As New Regex("^[^\s]+\s{1}\d{2}(\d{2}?)", RegexOptions.Compiled Or RegexOptions.CultureInvariant)
        Dim streamReader As New StreamReader(fileName)
        Dim line As String = String.Empty
    
        Do While streamReader.Peek >= 0
            line = streamReader.ReadLine()
            MessageBox.Show(rx.Matches(line)(0).Groups(1).Value)
        Loop 
    End Sub
    
    我并不是说这是唯一(或最优雅)的正则表达式,但它会起作用,意味着您不必使用子字符串,也不在乎第一个字段的长度。它还假定字段之间只有一个空格,但也可以根据需要进行更改。只要你能制定出一个规则去