各执基于字符串" |"符&安培;设定值不同的阵列 [英] Split a string based on "|" delimiter & set values to different Arrays

查看:121
本文介绍了各执基于字符串" |"符&安培;设定值不同的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我使用在我的VB.Net项目中的配置文件如下code:

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

[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

我所想要做的是第一部分去叫butNum(数组),第二部分去叫butVal()数组,第三去butLink,四去butDes()。在code我至今如下:

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:

    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

由于轻微的一边,将我的配置文件字符串值需要在的(即 1 |测试按钮|C:\\ TEMP \\ test.csv...... )?我会假设不是,但它耍赖我:P

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

推荐答案

看一看采用分体式字符串函数您的第二道防线。

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

另外,作为基督徒提到,如果你可以改变格式,使所有的信息都在一行用户1 | 1 |测试按钮| ......会更好,因为你可以使用分割功能,并得到一个所有细节去了。

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.

采用分体式示例

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是你的secondlinestring的每个分割的数组,以便strarr(0)=1strarr(1)=测试按钮等。

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

正如你所看到具有隔开一行的所有细节|将节省您不必编写code,以便第一和第二线的差异。

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.

这篇关于各执基于字符串&QUOT; |&QUOT;符&安培;设定值不同的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆