Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在字符串中查找空格,删除其后的所有内容_Vb.net_Visual Studio 2010_Variables_Strip - Fatal编程技术网

Vb.net 在字符串中查找空格,删除其后的所有内容

Vb.net 在字符串中查找空格,删除其后的所有内容,vb.net,visual-studio-2010,variables,strip,Vb.net,Visual Studio 2010,Variables,Strip,希望这会很简单,但事实似乎并非如此 我在vb.net“contactname”中有一个变量。 格式就像“约翰·史密斯” 我只想从中得到名字,但似乎做不到 我已经找到并改编了一些来自谷歌的例子,但似乎没有任何效果:(只需空格上的字符串并取第一个元素: contactname.Split(" "c)(0) Dim forename作为字符串 Dim i=contactname.IndexOf(“”) 如果i-1那么 forename=contactname.Substring(0,i) MsgBo

希望这会很简单,但事实似乎并非如此

我在vb.net“contactname”中有一个变量。 格式就像“约翰·史密斯”

我只想从中得到名字,但似乎做不到

我已经找到并改编了一些来自谷歌的例子,但似乎没有任何效果:(

只需空格上的字符串并取第一个元素:

contactname.Split(" "c)(0)
Dim forename作为字符串
Dim i=contactname.IndexOf(“”)
如果i-1那么
forename=contactname.Substring(0,i)
MsgBox(名字)
如果结束

试试看

如果您愿意,可以使用正则表达式:

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

这正是我想要的——谢谢你sir@John-这就是
(0)
的作用。Split返回一个数组,据我记忆所及,在VB中访问数组元素是通过
()
@damien不信者,你能告诉我“c”代表什么吗?@chiapa-在VB中,
是一个
字符串c
是一个
字符
。在c中,它们使用不同的分隔符(
vs
),但在VB中,它们使用相同的分隔符和后缀。感谢您的解释。您还需要处理“John Paul Smith”或“P.John Smith”吗?Dim i=contactname.IndexOf(“”)。这行有问题
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