我有以下函数似乎工作。这是在设计代码:在字符串中查找字符串的第一个实例
Method findFirst(word As String) As Integer
foundPosition As integer
Set foundPosition To -1
wordLen As integer
Set wordLen To len(word)
startingPoint As integer
Set startingPoint To (len(Text)- 1) - wordLen
For iPosition As integer From startingPoint To 0 Step -1
If substring(iPosition, wordLen) = word Then
foundPosition = iPosition
End If
Next iPosition
Return foundPosition
End Method
在VB.NET
实现我有以下几点:
Public Function findFirst(word As String) As Integer
Dim foundPosition As Integer = -1
Dim wordLen As Integer = word.Length
Dim startingPoint As Integer = (fText.Length - 1) - wordLen
For iPosition As Integer = startingPoint To 0 Step -1
If fText.Substring(iPosition, wordLen) = word Then
foundPosition = iPosition
End If
Next iPosition
Return foundPosition
End Function
它返回现场用FText内的参数字的位置。
这是一个有效的方法吗?
它容易破裂吗?
有更好的解决方案吗?
[''String.IndexOf'](http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx)? – 2013-05-08 07:27:59
如果你必须重新实现它,在另一个方向运行循环(从0到'startingPoint',你可能要重新命名),并改变'foundPosition = iPosition'到'返回iPosition' – 2013-05-08 07:31:30
@Damien_The_Unbeliever你的第二个评论是我之后的信息片断....有一种感觉,我错过了一些明显的东西。 – whytheq 2013-05-08 11:46:19