使用Visual Basic 2010的Im我有用戶輸入字符串,好的子字符串和壞的子字符串(三個文本框)。我需要讓我的代碼在我的字符串中找到壞子字符串的實例,並用良好的子字符串替換每個實例。我覺得應該有一個循環來確定壞字符串的實例。我無法圍繞它思考。這是我迄今爲止所有的。希望有人能幫助我。Visual Basic 2010用壞的子字符串替換好的子字符串
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
Dim strEnteredString As String = txtInputString.Text.Trim
Dim intEnteredStringChars As Integer = strEnteredString.Length
Dim strGoodSubstring As String = txtInputGoodString.Text.Trim
Dim intGoodSubstringChars As Integer = strGoodSubstring.Length
Dim strBadSubstring As String = txtInputBadString.Text.Trim
Dim intBadSubstringChars As Integer = strBadSubstring.Length
Dim strNewString As String = strEnteredString
Dim strNewStringFinal As String
If strEnteredString.Contains(strBadSubstring) Then
strNewStringFinal = strNewString.Remove(
strNewString = strEnteredString.Insert(intBadSubstringChars, strGoodSubstring)
End If
示例數據將非常有用。你可以使用'String.Replace'來替換所有不好的(好的)子字符串。這聽起來好像你的問題可能與[我自己的]相關(http://stackoverflow.com/questions/9264262/replacing-bad-characters-of-a-string-with-bad-characters)。 –
只需使用String.Replace()方法。 –