string 在某些单词 vb.net 之后或之前剥离字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17152541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:58:40 来源:igfitidea点击:
Strip String after or before certain word vb.net
提问by user2494189
I want to cut a string and take what is before a certain word and what is after a certain word.
我想剪一个字符串,取某个单词之前的内容和某个单词之后的内容。
Example:
例子:
Dim string As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim string_before, string_after As String
--cutting code here--
string_before = "Dr. John "
string_after = " 123 Main Street 12345"
How would I do this in vb.net?
我将如何在 vb.net 中做到这一点?
回答by matzone
You can use split() function or this
您可以使用 split() 函数或此
Dim mystr As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim x As Integer = InStr(mystr, cut_at)
Dim string_before As String = mystr.Substring(0, x - 2)
Dim string_after As String = mystr.Substring(x + cut_at.Length-1)
回答by Reed Copsey
You could use String.Split:
你可以使用String.Split:
Dim original As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim stringSeparators() As String = {cut_at}
Dim split = original.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
Dim string_before = split(0)
Dim string_after = split(1)