VB.net获取指定字符串后的第一个单词
我只需要在指定的字符串之后获取第一个单词,如so pseudo:VB.net获取指定字符串后的第一个单词,vb.net,string,split,substring,Vb.net,String,Split,Substring,我只需要在指定的字符串之后获取第一个单词,如so pseudo: my_string = "Hello Mr. John, how are you today?" my_search_string = "are" result = "you" 我试着用下面的方法来做,但是我得到了我的关键字字符串后面的字符串的其余部分,而不是一个单词 Dim search_string As String = "key" Dim x As Integer = InStr(Textbox1.tex
my_string = "Hello Mr. John, how are you today?"
my_search_string = "are"
result = "you"
我试着用下面的方法来做,但是我得到了我的关键字字符串后面的字符串的其余部分,而不是一个单词
Dim search_string As String = "key"
Dim x As Integer = InStr(Textbox1.text, search_string)
Dim word_after_key As String = Textbox1.text.Substring(x + search_string.Length - 1)
试试这个:
Dim str = "Hello Mr. John, how are you today?"
Dim key = " are "
Dim i = str.IndexOf(key)
If i <> -1 Then
i += key.Length
Dim j = str.IndexOf(" ", i)
If j <> -1 Then
Dim result = str.Substring(i, j - i)
End If
End If
或许这是:
Dim str = "Hello Mr. John, how are you today?"
Dim key = "are"
Dim words = str.Split(" "C)
Dim i = Array.IndexOf(words, key)
If i <> -1 AndAlso i <> words.Length - 1 Then
Dim result = words(i + 1)
End If
这也行得通
Dim my_string as String = "Hello Mr. John, how are you today?"
Dim SearchString As String = "are"
Dim StartP As Integer = InStr(my_string, SearchString) + Len(SearchString) + 1
' to account for the space
If StartP > 0 Then
Dim EndP As Integer = InStr(StartP, my_string, " ")
MsgBox(Mid(my_string, StartP, EndP - StartP))
End If
Psst:使用String.IndexOf而不是InStr。您需要在找到的索引之后查找下一个单词边界。这可以是一个空格,任何类型的标点符号,也可以是你定义的任何东西。这个答案可能是正确的,并为这个问题提供了一个解决方案。但是你可以添加解释,说明它是如何解决问题的,这样未来的读者就可以了解它是如何工作的。当我运行这个时,我得到的返回是打开的。我遗漏了什么吗?你可以去掉单词open前面的括号。。。因为s将返回括号后的内容。。。
Dim sa As String
Dim s As String
Dim sp() As String
sa = TextBox1.Text 'this text box contains value **Open Ended Schemes(Debt Scheme - Banking and PSU Fund)**
sp = sa.Split("(") 'Here u get the output as **Debt Scheme - Banking and PSU Fund)** which means content after the open bracket...
sp = sp(1).Split(")") 'Here u get the output as Debt Scheme - Banking and PSU Fund which means content till the close bracket...
s = Split(sp(0))(0) 'Here it will take the first word, which means u will get the output as **Debt**
s = Split(sp(0))(1) 'Change the index as per the word u want, here u get the output as **Scheme**