简体   繁体   中英

how to extract values from a string in vb.net?

i have this string "Raw: 463 -22 -17Raw: 463 -22 -17Raw:", but i only want to get "463 -22 -17" etc, basically just the numeric value. how do i do extract that from a string? by the way, there are 3 spaces between the first number and second number, same thing for the second and third number. however, there is no space between the third and next number. this pattern keeps repeating. please help me. thanks in advance cheers!

You can use REPLACE.

     Dim x As String = "Raw: 463 -22 -17Raw: 463 -22 -17Raw:"
     x = Replace(x, "Raw: ", " ") '<-I added an empty space as replacement, but you can use whatever you like.
     MsgBox(x)

Try this:

Dim values = text.Split( _
    New String() { "Raw:" }, _
    StringSplitOptions.RemoveEmptyEntries)

From your sample data you get this:

结果

If you want to get all the values out as integers you could do this:

Dim values = _
    From numbers in text.Split( _
        New String() { "Raw:" }, _
        StringSplitOptions.RemoveEmptyEntries) _
    Select numbers.Split( _
        New Char() { " "c }, _
        StringSplitOptions.RemoveEmptyEntries) _
            .Select(Function (x) Integer.Parse(x))

And that gives you:

结果2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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