【问题标题】:Visual Basic Not using Replace or remove, only Length, Substring and IndexofVisual Basic 不使用 Replace 或 remove,仅使用 Length、Substring 和 Indexof
【发布时间】:2015-11-19 23:37:50
【问题描述】:

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

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

输出将是:“我喜欢 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

【问题讨论】:

  • Indexof 不是 vba 函数。你是说vb.net吗? vbavb.net。如果您的标签准确无误,您将获得更好的帮助。
  • 我不明白; “仅使用预定义的字符串方法” Replace 是那些预定义的方法之一;它有什么问题?

标签: vb.net string replace indexof


【解决方案1】:
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

【讨论】:

  • 谢谢@Sehnsucht!它只适用于一个首字母缩略词 - 我正在按钮中尝试 For Loop 让它 > 1...(如果我输入输入“五辆自行车店,五辆自行车店”,它会出现“5BS , 5BS" Private Sub btconvert_Click(sender As Object, e As EventArgs) Handles btconvert.Click Dim input, Output, old, newer As String Dim P As Integer Dim L As Integer input = CStr(txtIn.Text) L = input.Length old = "Five Bike Shop" newer = "5BS" For P = 1 To L - 1 Output = MyReplace(input, old, newer) txtOut.Text = Output Next
  • 什么?请把同样的事情说清楚好吗?如果你想说的是它只替换了一次;然后I beg to differ 行为(除非我犯了错误)与 String.Replace 相同(只是缺少参数验证)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 2014-05-27
  • 2013-04-24
  • 2019-10-04
  • 2011-08-18
  • 2016-10-04
相关资源
最近更新 更多