如何获取拆分字符串数组的最后一个值?[VB.net]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23249014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get last value of split string-array? [VB.net]
提问by user3565290
Alright so I have this:
好的,所以我有这个:
Dim OldString As String = "Word<>Car<>Test<>"
Dim NewString As String() = OldString.Split("<>")
Now I want to get the last value of OldString, which would be "Test", just before you ask, the last "<>" after "Test" has to be there, no matter what. Everytime a new string would get added to the array, it would add "VALUE<>", in my case there's no way around it.
现在我想获得 OldString 的最后一个值,也就是“Test”,就在你问之前,“Test”之后的最后一个“<>”必须在那里,无论如何。每次将新字符串添加到数组时,它都会添加“VALUE<>”,在我的情况下没有办法绕过它。
I only need the last value, but without knowing how many values the string-array has,
means I cannnot do something like NewString(2)or similar, since I don't know how many
values the string-array has...
我只需要最后一个值,但不知道字符串数组有多少个值,意味着我不能做类似NewString(2)或类似的事情,因为我不知道字符串数组有多少个值......
I am stuck and I've been trying things for hours, I hope you guys can help me out, thanks!
我被卡住了,我已经尝试了几个小时,希望你们能帮助我,谢谢!
采纳答案by Rick S
You need to fix your split statement:
您需要修复 split 语句:
Dim NewString As String() = OldString.Split(New String() {"<>"}, StringSplitOptions.None)
You can loop through the NewString arrary like this:
您可以像这样遍历 NewString 数组:
For Each lstrString as String in NewString
if ( String.IsNullOrEmpty(lstrString) = False) then
Console.WriteLine("This is a value: " + lstrString)
end if
Next
'Get the length of NewString like this
'像这样获取NewString的长度
Dim lngth as integer = NewString.length()
'Get the last value
'获取最后一个值
Dim lstrLastValue as string = NewString(lngth - 1) 'Or maybe -2 in your case

