vb中的string操作方法提供了mid(从string1中截取需要的一段string)的功能,但是实际程序中需要从string1中返回去除string2的字符串,vb代码如下,大家可以模拟改成其他语言。
Public Function RemoveString(SourceString As String, CutString As String, Optional iStart As Long = 1, Optional iSkipCount As Integer = 0) As String
'删除 SourceString 中的 RemoveString ,从 iStart 开始查找 RemoveString,跳过 iSkipCount 个不删除
Dim startPos As Long, endPos As Long, iSkip As Integer
Dim str1 As String, str2 As String
iSkip = iSkipCount
startPos = VBA.InStr(iStart, SourceString, CutString, vbTextCompare)
Do While startPos > 0 And iSkip > 0
startPos = VBA.InStr(startPos + 1, SourceString, CutString, vbTextCompare)
iSkip = iSkip - 1
Loop
If startPos > 0 And iSkip = 0 Then
str1 = VBA.Left(SourceString, startPos - 1)
str2 = VBA.Right(SourceString, VBA.Len(SourceString) - VBA.Len(str1 + CutString))
RemoveString = str1 + str2
Else
RemoveString = SourceString
End If
End Function
'删除 SourceString 中的 RemoveString ,从 iStart 开始查找 RemoveString,跳过 iSkipCount 个不删除
Dim startPos As Long, endPos As Long, iSkip As Integer
Dim str1 As String, str2 As String
iSkip = iSkipCount
startPos = VBA.InStr(iStart, SourceString, CutString, vbTextCompare)
Do While startPos > 0 And iSkip > 0
startPos = VBA.InStr(startPos + 1, SourceString, CutString, vbTextCompare)
iSkip = iSkip - 1
Loop
If startPos > 0 And iSkip = 0 Then
str1 = VBA.Left(SourceString, startPos - 1)
str2 = VBA.Right(SourceString, VBA.Len(SourceString) - VBA.Len(str1 + CutString))
RemoveString = str1 + str2
Else
RemoveString = SourceString
End If
End Function