Split a string based on “|” delimiter & set values to different Arrays

时间:2021-11-07 22:09:10

So I have the following code that I am using as a config file in my VB.Net project:

所以我在我的VB.Net项目中使用以下代码作为配置文件:

[User 1]
1|Test Button|C:\temp\test.csv|This Button is a test button
2|Test Button 2|C:\temp\test.csv|This Button is another test button

[User 2]
... etc

What I am wanting to do is have the first part go to an array called butNum(), second part go to an array called butVal(), third to go to butLink and 4th to go to butDes(). The code I have so far is as follows:

我想要做的是让第一部分转到名为butNum()的数组,第二部分转到名为butVal()的数组,第三部分转到butLink,第四部分转到butDes()。我到目前为止的代码如下:

    While Not reader.EndOfStream
        line = reader.ReadLine()
        If line.StartsWith("[") Then
            Dim Firstpart As String = Nothing
            Firstpart = line.Replace("[", "")
            Firstpart = Firstpart.Replace("]", "")
            If Firstpart = login.Login1.Text Then 'this checks the user has logged in as "User 1"
                While reader.ReadLine() <> ""
                    ReDim Preserve butNum(0 To lines)
                    ReDim Preserve butVal(0 To lines)
                    ReDim Preserve butLink(0 To lines)
                    ReDim Preserve butDes(0 To lines)
                    butNum(lines) = 'part 1 delimited
                    butVal(lines) = 'part 2 delimited
                    butLink(lines) = 'part 3 delimited
                    butDes(lines) = 'part 4 delimited
                    line = reader.ReadLine()
                    lines = lines + 1
                End While
            End If
            Application.DoEvents()
        Else
            Application.DoEvents()
        End If
    End While

As a slight aside, would my config file string values need to be in ""'s (ie 1|"Test Button"|"C:\temp\test.csv"...)? I would assume not, but its niggling me :P

稍微一点,我的配置文件字符串值是否需要在“”中(即1 |“测试按钮”|“C:\ temp \ test.csv”......)?我不会假设,但是它让我唠叨:P

1 个解决方案

#1


1  

Have a look at using split string function for your second line.

看看你的第二行使用split string函数。

Also as Christian mentioned if you could change the format so that all information is in one line User1|1|Test Button|...... would be better as you could use the split function and get all details in one go.

同样正如Christian提到的那样,如果你可以改变格式,以便所有信息都在一行中,User1 | 1 | Test Button | ......会更好,因为你可以使用split函数并一次性获取所有细节。

Example of using split

使用拆分的示例

Dim secondlinestring As String = "1|Test Button|C:\temp\test.csv|This Button is a test button"
Dim strarr() As String
strarr = secondlinestring.Split("|"c)
For Each s As String In strarr
    MessageBox.Show(s)
Next

strarr is your array of each split of the secondlinestring so strarr(0) = "1" strarr(1) = "Test Button" etc.

strarr是你的第二个线串的每个分裂的数组,所以strarr(0)=“1”strarr(1)=“测试按钮”等。

As you can see having all details in one line separated by | would save you having to write code to allow for first and second line differences.

正如您所看到的那样,将一行中的所有细节分隔为|可以节省你必须编写代码以允许第一和第二行差异。

#1


1  

Have a look at using split string function for your second line.

看看你的第二行使用split string函数。

Also as Christian mentioned if you could change the format so that all information is in one line User1|1|Test Button|...... would be better as you could use the split function and get all details in one go.

同样正如Christian提到的那样,如果你可以改变格式,以便所有信息都在一行中,User1 | 1 | Test Button | ......会更好,因为你可以使用split函数并一次性获取所有细节。

Example of using split

使用拆分的示例

Dim secondlinestring As String = "1|Test Button|C:\temp\test.csv|This Button is a test button"
Dim strarr() As String
strarr = secondlinestring.Split("|"c)
For Each s As String In strarr
    MessageBox.Show(s)
Next

strarr is your array of each split of the secondlinestring so strarr(0) = "1" strarr(1) = "Test Button" etc.

strarr是你的第二个线串的每个分裂的数组,所以strarr(0)=“1”strarr(1)=“测试按钮”等。

As you can see having all details in one line separated by | would save you having to write code to allow for first and second line differences.

正如您所看到的那样,将一行中的所有细节分隔为|可以节省你必须编写代码以允许第一和第二行差异。