我有以下字符串:Anaheim Temp. 1-N/A
和Cevera-N/A
。
如何从第一个获得Temp
而从第二个获得Cevera
?
很抱歉,我会尝试提供更多信息:所以我想说我想将第一个字符串拆分为名字和姓氏,所以名字将是" Anaheim"姓氏将是" Temp"第二个例子:" Alex Orellana-NA"所以名字将是" Alex"姓氏" Orellana"我如何摆脱像#34; - "等特殊字符后出现的问题? "" " /"
答案 0 :(得分:0)
我想我理解你的要求。您正在寻找一个函数,它将使用2个字符串并使用短语搜索一个字符串,然后返回包含它的结果。我相信我的问题有解决方案:
Public Function FindCertainString(stringToSearch As String, stringToFind As String) As String
For index = 1 To stringToSearch.Length - 1
Dim character As Char = stringToSearch(index)
' Parse through whole string and find any match values
If character = stringToFind(0) Then
' Matched the first letter
Dim tempIndex As Integer = index
Dim someWord As String = ""
For innerIndex = 1 To stringToFind.Length
someWord += stringToSearch(tempIndex)
tempIndex += 1
Next
If someWord = stringToFind Then
' Found the word you were looking for
Return someWord
End If
End If
Next
Return ""
End Function
这应该满足你的要求,因为这个函数将做的是接受两个字符串然后为stringToSearch
它将删除每个字符,直到它识别与{{中的起始字母匹配1}}从那里它收集相同长度的字符并检查它是否与stringToFind
值匹配。
答案 1 :(得分:0)
信息:此答案已过时,基于编辑前OP的初始问题。
"Anaheim Temp. 1-N/A".IndexOf("Temp") ' -> returns 8 = found at index 8 !
"Cevera-N/A".IndexOf("Cevera") ' -> returns 0 = found at index 0 !
"Cevera-N/A".IndexOf("Temp") ' -> returns -1 = not found !
"Cevera-N/A".IndexOf("cevera") ' -> returns -1 = not found ! because it's lowercase !
我 同意上述评论:您没有提供查找指定字词的目的,或者您是否正在寻找复杂逻辑来确认字面上存在特定标识(Temp或Cereva或任何其他特定单词,在任何提供的字面句子或公式中)
我们不知道" Anaheim Temp的含义是什么。 1-N / A" ,它来自哪里,它是一个字符串变量的值或在文本框中输入的公式的一些文字表达...
你想找到" Temp"在" Anaheim Temp。 1-N / A" ,它已经!!!我们到底要怎样搜索远方,而显然" Anaheim Temp。 1-N / A" 包含" Temp" ?
有几种方法可以知道String是否包含另一个String,但有用的方法取决于字符串" Anaheim Temp的位置。 1-N / A" 来自和为什么字符串" Temp"应该在那里找到但不是" 1"或" -N"甚至" SantaMaria"以及搜索该词的目的......
You haven't tried anything到目前为止,如果我理解,你希望我们猜测背后的逻辑...... XD
答案 2 :(得分:0)
以下代码的作用是逐个提取名称组件,直到找到无效字符。
函数输入是String
,它包含文字名称。
输出是List(Of String)
,其中包含每个提取的名称组件。
样品测试 - >结果名称组件
"Anaheim Temp. 1-N/A" -> [Anaheim],[Temp]
"Cevera-N/A" -> [Cevera]
"Alex Orellana-NA" -> [Alex],[Orellana]
"Marie J Blige ?" -> [Marie],[J],[Blige]
"Marie J. Blige /vv" -> [Marie],[J.],[Blige]
"José F. 1452" -> [José],[F.]
"P. P. d'Arvor" -> [P.],[P.],[d]
制作" P. P. d' Arvor"工作,应该包括[']字符的处理,我没有。
(编辑:它不是关于子串的。)
Public Function GetNames(ByVal SampleName As String) As List(Of String)
Dim CharIndex As Integer = 0
Dim NameStart As Integer = 0
Dim NamesList As New List(Of String)
' Iterate the Chars from the left
While CharIndex < SampleName.Length
If Not Char.IsLetter(SampleName(CharIndex)) Then
If Char.IsWhiteSpace(SampleName(CharIndex)) Then
If CharIndex > NameStart Then
NamesList.Add(SampleName.Substring(NameStart, CharIndex - NameStart))
End If
NameStart = CharIndex + 1
Else
If (SampleName(CharIndex) = "."c) Then
' This, to allow examples like "Angelina J.-N/A"
' "Angelina" will be added first
' then "J." (with the dot) will be added aswell.
If NameStart = (CharIndex - 1) Then
' This simple check prevent the picking of
' two consecutive dots like
' "Jarod R.. Solo" -> "Jarod" and "R." ("Solo" discarded)
' "A. Leo D........" -> "A.", "Leo" and "D."
' ". Andrew" -> "" (break immediately)
NamesList.Add(SampleName.Substring(NameStart, CharIndex - NameStart + 1))
NameStart = CharIndex + 1
Else
If NameStart < CharIndex Then
NamesList.Add(SampleName.Substring(NameStart, CharIndex - NameStart))
End If
Exit While
End If
Else
If NameStart < CharIndex Then
NamesList.Add(SampleName.Substring(NameStart, CharIndex - NameStart))
End If
Exit While ' Discard the rest
End If
End If
End If
CharIndex = CharIndex + 1
End While
Return NamesList
End Function
然后使用这样的方法:
Public Sub TestNames()
Dim TestName As String
Dim NameComponents As List(Of String)
TestName = Microsoft.VisualBasic.InputBox("Enter Name") ' lazy me -_-
NameComponents = GetNames(TestName)
' wanna see preview ? un-comment the following.
'TestName = ""
'For Each Word As String In NameComponents
' TestName = TestName + "[" + Word + "]" + Environment.NewLine
'Next
'MessageBox.Show(TestName)
If NameComponents.Count > 1 Then ' You have at least two Names.
' The logic here is up to you
' FirstName = NameComponent.Item(0)
' LastName = NameComponent.Item(1)
ElseIf NameComponents.Count > 0 Then ' You have only one Name.
' FirstName = NameComponent.Item(0)
' LastName = ""
Else
' FirstName = "Unknown"
' LastName = ""
End If
End Sub
编辑:修复了多个连续点综合症。