简体   繁体   English

解析此示例字符串

[英]Parsing this example string

I'm trying to split up the following string into substrings 我正在尝试将以下字符串拆分为子字符串

Dim value as String = "+1.0    A0   -256   B0  -2823" 'this is a line from a txtfile

I want to split the string in the following 3 substrings. 我想将字符串拆分为以下3个子字符串。

Dim value1  as String= "+1.0"
Dim A0 as String = "-256" 
Dim B0 as String = "-2823"

the String is always in the following format: 字符串始终采用以下格式:

+## A0 #### B0 ### + ## A0 #### B0 ###

I know I can achieve it with the use of value.Replace and value.Substring , but I don't know how exactly. 我知道我可以配合使用实现它value.Replacevalue.Substring ,但我不知道究竟怎么了。

I have tried stuff like this, but it didn't work 我已经尝试过类似的东西,但是没有用

value1 = Value.Substring(0, "A0".IndexOf("A0"))

You can use String.Split here with respect to two strings ( A0 and B0 ), which will be the clearest solution I believe: 您可以在此处针对两个字符串( A0B0 )使用String.Split ,这是我认为最清晰的解决方案:

Dim value as String = "+1.0 A0 -256 B0 -2823"
Dim values = value.Split({ " A0 ", " B0 " }, StringSplitOptions.None)

NET contains a powerful String Split method. NET包含强大的String Split方法。 The source is delimited by spaces, so split on that and grab elements 0, 2 and 4 from the result: 源由空格分隔,因此在其上进行分割并从结果中获取元素0、2和4:

If/when the lines read have a single space between the items as described in one part: 如前所述,如果/当读取的行之间在项目之间有一个空格:

Dim value As String = "+1.0 A0 -256 B0 -2823"

Dim split = value.Split(" "c)
Dim val1 = split(0)            ' +1.0
Dim A0 = split(2)              ' -256
Dim B0 = split(4)              ' -2823

If/When there can be multiple spaces (as in top code sample), your comment about getting rid of them will still allow String.Split : 如果/当可以有多个空格时(如顶部代码示例中所示),关于删除它们的评论仍将允许String.Split

Dim value As String = "+1.0   A0          -256    B0    -2823"

Dim tmp = value.Replace(" ", "")    ' remove spaces, preserve original just in case
tmp = tmp.Replace("A0", " ")        ' replace A0 with space
tmp = tmp.Replace("B0", " ")
Dim split = tmp.Split(" "c)         ' split

Dim val1 = split(0)                 ' grab info
Dim A0 = split(1)
Dim B0 = split(2)

Any character you are sure wont be in the string can be used to replace A0 and B0. 您确定不会在字符串中的任何字符都可以用来替换A0和B0。 Since we just stripped out the spaces, that seems most logical. 由于我们只是删除了空格,所以这似乎是最合逻辑的。

You were on the right lines with the first attempt. 第一次尝试时您处在正确的位置。 You just need to use the actual string with the value in to find the location of the items. 您只需要使用带有值的实际字符串来查找项目的位置。 As you have un potentially unknown number of spaces, you need to useTrim to remove the spaces afterwards: 由于您的空格数可能未知,因此需要使用Trim删除空格:

    Dim value As String = "+1.0    A0   -256   B0  -2823" 'this is a line from a txtfile

    Dim value1 As String = value.Substring(0, value.IndexOf(" "c))
    Dim aPos As Integer = value.IndexOf("A0") 'Find the position of "A0" in the string
    Dim bPos As Integer = value.IndexOf("B0") 'Find the position of "B0" in the string
    Dim A0 As String = value.Substring(aPos + 2, bPos - (aPos + 2)).Trim
    Dim B0 As String = value.Substring(bPos + 2).Trim

If you want to split, try Split function ;) 如果要拆分,请尝试拆分功能;)

Split(expression as string, delimiter as string) as string() 拆分(表达式作为字符串,定界符作为字符串)作为string()

You want to split your expression by " " and take 1st, 3rd and 5th element from result array (2nd is "A0" and 4th is "B0") So your code should look like : 您想用“”分割表达式,并从结果数组中获取第1,第3和第5个元素(第2个为“ A0”,第4个为“ B0”),因此您的代码应类似于:

Option base 0    
' so we start counting at 0

Dim results as String()
results = Split(value," ")
value1 = results(0) 
A0 = results(2)
B0 = results(4)

Per my comment, using replace and split 根据我的评论,使用替换和拆分

String s = "+1.0 A0 -256 B0 -2823";
            s = s.Replace("A0", "").Replace("B0", "");
            String[] allNumbers = s.Split(' ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM