Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

.net - Split a string into parts

I am trying to split a string into parts but can't figure it out!

My main point is from a string

"hello bye see you" 

read from "bye" to "you"

I tried

 Dim qnew() As String = tnew.Split(" ")

But I got stuck on other parts of the code, I would really like some help. Sorry if I'm not the best at explaining things, at least I tried my best :/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume that your expected output is bye see you.If I understood correctly then following methods can be used to get the desired output:

In this string splits into an array(splits()) with delimiter " " and find index of bye (j)and you(k) in the array then using a for loop to get strings in the array between bye and you.

Function GETSTRINGBETWEEN(ByVal start As String, ByVal parent As String, ByVal [end] As String)
        Dim output As String = ""
        Dim splits() As String = parent.Split(" ")
        Dim i As Integer
        Dim j As Integer = Array.IndexOf(splits, start)
        Dim k As Integer = Array.IndexOf(splits, [end])
        For i = j To k
            If output = String.Empty Then
                output = splits(i)
            Else
                output = output & " " & splits(i)
            End If
        Next
        Return output
    End Function

Usage:

Dim val As String
val = GETSTRINGBETWEEN("bye", "hello bye see you", "you")
'val="bye see you"

Function GET_STRING_BETWEEN(ByVal start As String, ByVal parent As String, ByVal [end] As String)
        Dim output As String
        output = parent.Substring(parent.IndexOf(start) _
                                                , (parent.IndexOf([end]) _
                                                   - parent.IndexOf(start)) _
                                                   ).Replace(start, "").Replace([end], "")
        output = start & output & [end]
        Return output
    End Function

Usage:

Dim val As String
val = GET_STRING_BETWEEN("bye", "hello bye see you", "you")
'val="bye see you"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...