Vb.net 如何拆分字符串以获取字符串中的最后一个单词或两个单词,但不知道字符数或结束单词数?

Vb.net 如何拆分字符串以获取字符串中的最后一个单词或两个单词,但不知道字符数或结束单词数?,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,要比标题更具体。。。下面是一个要使用的字符串示例:“You have received Doe from John Doe”根据字符串的名或名和姓,我需要只获取John或John Doe的名字。下面是我在字符串中显示John Doe的代码,但它只得到John,而不是John Doe的全名。我正在使用Visual Basic 2010。有人能帮忙吗 Dim myString As String = "You have received 25 dollars from John Doe" Dim

要比标题更具体。。。下面是一个要使用的字符串示例:“You have received Doe from John Doe”根据字符串的名或名和姓,我需要只获取John或John Doe的名字。下面是我在字符串中显示John Doe的代码,但它只得到John,而不是John Doe的全名。我正在使用Visual Basic 2010。有人能帮忙吗

Dim myString As String = "You have received 25 dollars from John Doe"

Dim fields() As String = myString.Split(" ")

Dim numberDollars As String = fields(3).Substring(0)

Dim nameDonated As String = fields(6).Substring(0)

' outputs John donated 25 dollars
TextBox1.Text = nameDonated & " donated " & numberDollars & " dollars."

因为它总是采用相同的格式,“您从y收到了x美元”,所以可以基于该格式拆分字符串

Dim myString As String = "You have received 25 dollars from John Doe"
' split into {"You have received 25 dollars", "John Doe"}
Dim mySplitString1 As String() = myString.Split(New String() {" from "}, 0)
' and take the second item which has the name
Dim donorName As String = mySplitString1(1)
' then split the first item into {"You", "have", "received", "25", "dollars"}
Dim mySplitString2 As String() = mySplitString1(0).Split(" ")
' and take the fourth item which has the amount
Dim dollarAmount As Single = Single.Parse(mySplitString2(3))

TextBox1.Text = String.Format("{0} donated {1:0} dollars", donorName, dollarAmount)
有时候最简单的答案就是最好的。使用原始代码,将名称分配更改为

Dim nameDonated As String = fields(6) & If(fields.Length = 8, " " & fields(7), "")

你明白为什么用“john”代替全名吗?是的,字段6进入第六个空格并从那里开始。。。子字符串0是下一个单词的结尾。我需要的代码,以知道是否有2个字或1个字,这取决于该人输入的只是名字或名字和姓氏。我想我可以做一个nameDonated1和nameDonated2,然后模糊另一个语句,把它们放在一起。。。但我希望有一种比这更好的方法,在网络中没有任何东西可以区分动词、名词、冠词和其他任何东西。此外,你的
子字符串
没有做任何你想做的事情。。。我试着去掉了。子字符串,但它仍然有效。感谢您解释我的子字符串没有做任何事情。它总是以“您从y收到了x美元”的形式出现吗?
Dim name捐赠为String=fields(6)&If(fields.Length=8,“&fields(7),”)
works tooDim name捐赠为String=fields(6)&If(fields.Length=8,“&fields(7),”)工作起来就像做梦一样。非常感谢Verdolino对我的帮助。:)