Visual Basic不使用替换或删除,只使用Length,Substring和Indexof

时间:2015-11-19 23:37:50

标签: vb.net string replace indexof

尝试编写一个程序来替换和删除某些单词而不用替换或删除方法。仅使用以下函数:Length,Substring,IndexOf。

示例:我输入输入“我喜欢Fives自行车店的自行车。”

输出结果是:“我喜欢5BS的自行车。”

代码如下:

S = txtIn.Text
'Put a space
S = " " & S
output = ""
Acro = ""
Dim L As Integer
L = S.Length
Dim P As Integer
Dim ending, beginning As Integer

For P = 1 To L - 1
    If Acro.Length >= 25 Then
        ending = S.IndexOf("Five Bike Shop", beginning)
        beginning = S.IndexOf("s", beginning) + 1
        Acro = S.Substring(ending)
        output = output & S.Substring(ending) & "5BS"
    End If
Next
txtOut.Text = output
End Sub

End Class

1 个答案:

答案 0 :(得分:3)

Function MyReplace(input As String, oldValue As String, newValue As String) As String
    Dim index = 0
    Dim output = ""
    Dim searchIndex = input.IndexOf(oldValue)

    Do Until searchIndex = -1
        output &= input.SubString(index, searchIndex - index) & newValue
        index = searchIndex + oldValue.Length
        searchIndex = input.IndexOf(oldValue, index)
    Loop

    Return output & input.Substring(index)
End Function