当前位置: 动力学知识库 > 问答 > 编程问答 >

How to get last value of split string-array? [VB.net]

问题描述:

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.

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...

I am stuck and I've been trying things for hours, I hope you guys can help me out, thanks!

网友答案:

You need to fix your split statement:

Dim NewString As String() = OldString.Split(New String() {"<>"}, StringSplitOptions.None)

You can loop through the NewString arrary like this:

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

Dim lngth as integer = NewString.length()

'Get the last value

Dim lstrLastValue as string = NewString(lngth - 1) 'Or maybe -2 in your case
分享给朋友:
您可能感兴趣的文章:
随机阅读: