AppendLine 和替换
AppendLine and replacing
我想从
获得我的输出
223-F456
至
223-F456
223-F456#
223-F4560
223-#456
我为此编写了简单的代码,但仅在行尾添加 #,0 即可:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For Each line In TextBox1.Lines
sb.AppendLine(line)
sb.AppendLine(line & "#")
sb.AppendLine(line & "0")
sb.Replace(line.LastIndexOf("-") + 1, "#") -> this doesn't work
Next
TextBox2.Text = sb.ToString
输出:
223-F456
223-F456#
223-F4560
字母不总是"F":我想替换“-”后的第一个字母而不是替换"F",也可能有些连续剧有"F"不是我想要的字母.
商店中的简单产品系列但替换“-”后的字符串不起作用且不显示,我们将不胜感激。
问题是
line.LastIndexOf("-") + 1
将 return 一个数字(表示在 line
中找到最后一个 -
字符的位置加一)。
但是替换函数希望您告诉它您要替换的字符串。您要替换 F
,而不是数字。 (您当前的代码将解析为 sb.Replace(4, "#")
,显然找不到任何要替换的 4
。)
如果您这样做 sb.Replace
,它将替换您之前添加到 stringbuilder 的所有版本的字符串。因此,在 line
中替换它更有意义,然后将 line
的修改版本附加到 stringbuilder。
sb.AppendLine(line.Replace("F", "#"))
现场演示:https://dotnetfiddle.net/5BZAUg
编辑:
如果您需要替换 -
之后的任何字符,而不是特定的 F
,您可以通过查找 line
中的字符索引,然后删除该字符来实现在该索引处并将其替换为另一个索引,例如
Dim index = line.LastIndexOf("-") + 1
sb.AppendLine(line.Remove(index, 1).Insert(index, "#"))
您可以这样使用 String.Remove and String.Insert:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder()
For Each line In TextBox1.Lines
sb.AppendLine(line)
sb.AppendLine(line & "#")
sb.AppendLine(line & "0")
Dim replaceAt = line.LastIndexOf("-") + 1
sb.AppendLine(line.Remove(replaceAt, 1).Insert(replaceAt, "#"))
Next
TextBox2.Text = sb.ToString()
End Sub
或者,如果您愿意,可以使用 String.Substring 函数:
sb.AppendLine(line.Substring(0, replaceAt) & "#" & line.Substring(replaceAt + 1))
也可以使用正则表达式来进行替换,我希望其他人可以想出更好的正则表达式,但这行得通:
Dim re As New Regex("(.*-)([^-])(.*)")
For Each line In TextBox1.Lines
' ...
sb.AppendLine(re.Replace(line, "#"))
我想从
获得我的输出223-F456
至
223-F456
223-F456#
223-F4560
223-#456
我为此编写了简单的代码,但仅在行尾添加 #,0 即可:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For Each line In TextBox1.Lines
sb.AppendLine(line)
sb.AppendLine(line & "#")
sb.AppendLine(line & "0")
sb.Replace(line.LastIndexOf("-") + 1, "#") -> this doesn't work
Next
TextBox2.Text = sb.ToString
输出:
223-F456
223-F456#
223-F4560
字母不总是"F":我想替换“-”后的第一个字母而不是替换"F",也可能有些连续剧有"F"不是我想要的字母.
商店中的简单产品系列但替换“-”后的字符串不起作用且不显示,我们将不胜感激。
问题是
line.LastIndexOf("-") + 1
将 return 一个数字(表示在 line
中找到最后一个 -
字符的位置加一)。
但是替换函数希望您告诉它您要替换的字符串。您要替换 F
,而不是数字。 (您当前的代码将解析为 sb.Replace(4, "#")
,显然找不到任何要替换的 4
。)
如果您这样做 sb.Replace
,它将替换您之前添加到 stringbuilder 的所有版本的字符串。因此,在 line
中替换它更有意义,然后将 line
的修改版本附加到 stringbuilder。
sb.AppendLine(line.Replace("F", "#"))
现场演示:https://dotnetfiddle.net/5BZAUg
编辑:
如果您需要替换 -
之后的任何字符,而不是特定的 F
,您可以通过查找 line
中的字符索引,然后删除该字符来实现在该索引处并将其替换为另一个索引,例如
Dim index = line.LastIndexOf("-") + 1
sb.AppendLine(line.Remove(index, 1).Insert(index, "#"))
您可以这样使用 String.Remove and String.Insert:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder()
For Each line In TextBox1.Lines
sb.AppendLine(line)
sb.AppendLine(line & "#")
sb.AppendLine(line & "0")
Dim replaceAt = line.LastIndexOf("-") + 1
sb.AppendLine(line.Remove(replaceAt, 1).Insert(replaceAt, "#"))
Next
TextBox2.Text = sb.ToString()
End Sub
或者,如果您愿意,可以使用 String.Substring 函数:
sb.AppendLine(line.Substring(0, replaceAt) & "#" & line.Substring(replaceAt + 1))
也可以使用正则表达式来进行替换,我希望其他人可以想出更好的正则表达式,但这行得通:
Dim re As New Regex("(.*-)([^-])(.*)")
For Each line In TextBox1.Lines
' ...
sb.AppendLine(re.Replace(line, "#"))