Reset counter at line break

时间:2018-06-18 11:42:20

标签: string vb.net line-breaks

I'm developing a VB program, and I'm analyzing a set of strings. These string are 6 numbers on a row separated by a space.

5 1.667e-02 5.08 4.98 0.01112

I want to count the spaces and at line break reset the counter

If Mid(line, g, 1) = " " Then
    Dim h As integer
    h = h + 1  
End If 

How do I do this? Is it possible on VB?

2 个答案:

答案 0 :(得分:1)

要计算char的出现次数,可以使用LINQ

Dim str As String = "5 1.667e-02 5.08 4.98 0.01112"
Dim h As Integer = str.Count(Function(c) c = " "c)

答案 1 :(得分:0)

试试这个:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myStringSplitLine
    Dim myStringSplit
    Dim myStringResult As String = ""
    Dim myString As String = "5 1.667e-02 5.08 4.98 0.01112" & vbCrLf & "5 1.667e-02 5.08 4.98 0.01112"
    myStringSplitLine = myString.Split(vbCrLf)
    Dim i As Integer
    Dim j As Integer
    For j = 0 To UBound(myStringSplitLine)
        myStringSplit = myStringSplitLine(j).Split(" ")
        For i = 0 To UBound(myStringSplit)

            If i = UBound(myStringSplit) Then
                myStringResult = myStringResult & myStringSplit(i)
            Else
                myStringResult = myStringResult & myStringSplit(i) & vbCrLf
            End If
        Next
        MessageBox.Show("Count:" & i - 1 & " " & vbCrLf & myStringResult)
        myStringResult = ""
    Next
End Sub