我想要做的是更换所有' A'在" Bb"的字符串中。但它只会循环使用不在新字符串上的原始字符串。
例如:
AAA
BbAA
BbBbA
并且它会在那里停止,因为原始字符串的长度只有3.它只能读取第3个索引,而不能读取其余索引。
Dim txt As String
txt = output_text.Text
Dim a As String = a_equi.Text
Dim index As Integer = txt.Length - 1
Dim output As String = ""
For i = 0 To index
If (txt(i) = TextBox1.Text) Then
output = txt.Remove(i, 1).Insert(i, a)
txt = output
TextBox2.Text += txt + Environment.NewLine
End If
Next
End Sub
答案 0 :(得分:0)
检查您的评论以获得更好的解决方案,但是为了将来参考,您应该使用while循环而不是for循环,如果您的条件会发生变化并且您想要考虑该变化。
我在下面做了一个简单的例子来帮助你理解。如果您使用for循环尝试相同的操作,则只会打印“one”“two”和“three”,因为for循环“看不到”vals
已更改
Dim vals As New List(Of String)
vals.Add("one")
vals.Add("two")
vals.Add("three")
Dim i As Integer = 0
While i < vals.Count
Console.WriteLine(vals(i))
If vals(i) = "two" Then
vals.Add("four")
vals.Add("five")
End If
i += 1
End While
答案 1 :(得分:0)
我打算写一个while循环来回答你的问题,但意识到(在其他人的帮助下)你可以.replace(x,y)
Output.Text = Input.Text.Replace("A", "Bb")
'Input = N A T O
'Output = N Bb T O
编辑:可能有更好的选择,但我很快就记下了这个循环,希望它有所帮助。
你已经说过你的新人并且不完全理解while loops
。因此,如果您不理解functions
pass arguments
或'Cut & Paste into an Event (Change textboxes to whatever you have input/output)
Dim Input As String = textbox1.Text
Do While Input.Contains("A")
Input = ChangeString(Input, "A", "Bb")
' Do whatever you like with each return of ChangeString() here
Loop
textbox2.Text = Input
对他们' Cut & Paste into Code somewhere (not inside another sub/Function)
Private Function ChangeString(Input As String, LookFor As Char, ReplaceWith As String)
Dim Output As String = Nothing
Dim cFlag As Boolean = False
For i As Integer = 0 To Input.Length - 1
Dim c As Char = Input(i)
If (c = LookFor) AndAlso (cFlag = False) Then
Output += ReplaceWith
cFlag = True
Else
Output += c
End If
Next
Console.WriteLine("Output: " & Output)
Return Output
End Function
,我建议您也这样做。
这是您的活动,可以是按钮点击或文本框文本更改。
b_Storage
这是你的函数,有3个参数和一个可以在你的代码中调用的返回值
std::vector<B>::iterator begin() {
return b_Storage.begin();
}
std::vector<B>::iterator end() {
return b_Storage.end();
}
答案 2 :(得分:0)
如果你想逐个替换而不是使用Replace函数,你可以使用while循环来查找搜索字符/字符串的索引,然后在该索引处替换/ insert。
Sub Main()
Dim a As String = String.Empty
Dim b As String = String.Empty
Dim c As String = String.Empty
Dim d As Int32 = -1
Console.Write("Whole string: ")
a = Console.ReadLine()
Console.Write("Replace: ")
b = Console.ReadLine()
Console.Write("Replace with: ")
c = Console.ReadLine()
d = a.IndexOf(b)
While d > -1
a = a.Remove(d, b.Length)
a = a.Insert(d, c)
d = a.LastIndexOf(b)
End While
Console.WriteLine("Finished string: " & a)
Console.ReadLine()
End Sub
输出看起来像这样:
Whole string: This is A string for replAcing chArActers.
Replace: A
Replace with: Bb
Finished string: This is Bb string for replBbcing chBbrBbcters.
答案 3 :(得分:0)
我认为这使我们找不到String.ReplaceFirst
函数。由于没有,我们可以写出这个功能。然后调用它的代码变得更具可读性,因为它很快就能明白它所做的事情(从函数名称开始)。
Public Function ReplaceFirst(searched As String, target As String, replacement As String) As String
'This input validation is just for completeness.
'It's not strictly necessary.
'If the searched string is "null", throw an exception.
If (searched Is Nothing) Then Throw New ArgumentNullException("searched")
'If the target string is "null", throw an exception.
If (target Is Nothing) Then Throw New ArgumentNullException("target")
'If the searched string doesn't contain the target string at all
'then just return it - were done.
Dim foundIndex As Integer = searched.IndexOf(target)
If (foundIndex = -1) Then Return searched
'Build a new string that replaces the target with the replacement.
Return String.Concat(searched.Substring(0, foundIndex), replacement, _
searched.Substring(foundIndex + target.Length, searched.Length - (foundIndex + target.Length)))
End Function
请注意,当您阅读下面的代码时,您甚至不必花费一些时间来弄清楚它在做什么。它的可读性。输入字符串包含&#34; A&#34;,替换第一个&#34; A&#34;与&#34; Bb&#34;。
Dim input as string = "AAA"
While input.IndexOf("A") > -1
input = input.ReplaceFirst(input,"A","Bb")
'If you need to capture individual values of "input" as it changes
'add them to a list.
End While
您可以优化或完全替换该功能。重要的是你的代码是可读的,有人可以告诉它做了什么,ReplaceFirst
函数是可测试的。
然后,让我们说你想要另一个能给你所有&#34;版本的功能&#34;您的输入字符串替换为目标字符串:
Public Function GetIterativeReplacements(searched As String, target As String, replacement As String) As List(of string)
Dim output As New List(Of String)
While searched.IndexOf(target) > -1
searched = ReplaceFirst(searched, target, replacement)
output.Add(searched)
End While
Return output
End Function
如果你打电话
dim output as List(of string) = GetIterativeReplacments("AAAA","A","Bb")
它将返回包含
的字符串列表BbAAA, BbBbAA, BbBbBbA, BbBbBbBb
保持方法简短几乎总是好的。如果它们开始变得太长,只需将它们分解为具有清晰名称的较小方法。这样你就不会试图阅读和关注并测试一个很长的功能。无论你是否是新的程序员,这都很困难。诀窍是不能创建我们理解的长而复杂的函数,因为我们编写它们 - 它创建了小而简单的函数,任何人都可以理解。