将一个字符串分成几部分
我试图将一个字符串分成几部分,但无法弄清楚!
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"
从再见"到你"
我试过了
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 :/
我假设你的预期输出是再见
.如果我理解正确,那么按照方法可以用于获得所需的输出:
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:
在这个字符串中分割成一个数组(splits()
) 与分隔符" "
并找到bye
(j
) 和 you
(k
) 然后使用 for 循环
来获取 bye 之间的数组中的字符串
和 你
.
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
用法:
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
用法:
Dim val As String
val = GET_STRING_BETWEEN("bye", "hello bye see you", "you")
'val="bye see you"