vb.net 在字符串中找到空格,删除它后面的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20992168/
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
Find space in string, remove everything after it
提问by John
Hoping this would be simple, but doesnt seem to be.
希望这会很简单,但似乎并非如此。
I've got a variable in vb.net 'contactname'. format is like "John Smith"
我在 vb.net 'contactname' 中有一个变量。格式就像“约翰史密斯”
I want to get just the forename from this, but cant seem to do it.
我只想从这个名字中得到名字,但似乎无法做到。
I've found and adapted some examples from google, but nothing seems to work :(
我从谷歌找到并改编了一些例子,但似乎没有任何效果:(
回答by Damien_The_Unbeliever
回答by Alex
Could use Regex if you like:
如果您愿意,可以使用正则表达式:
Public Shared Function RegexGetForename(ByVal str As String) As String
Dim a = New System.Text.RegularExpressions.Regex("^(\w+)")
If a.IsMatch(str) Then
Return a.Match(str).Value
Else
Return vbNull
End If
End Function
回答by chiapa
Dim forename as string
Dim i = contactname.IndexOf(" ")
If i <> -1 Then
forename = contactname.Substring(0, i)
MsgBox(forename)
End If
try it
尝试一下

