使用VB.NET我希望能够在一行代码中替换字符串中的一系列字符。
是的,例如:Dim charsToReplace as string = "acegi"
Dim stringToBeReplaced as string = "abcdefghijklmnop"
charsToReplace.ToArray().ForEach(Function (c) stringTobeReplaced = stringTobeReplaced.Replace(c, ""))
然而,这不起作用。
以下工作正常,但我不希望该字符串是类级变量:
Sub Main()
Dim toReplace As String = "acegikmoq"
Console.WriteLine(mainString)
Dim chars As List(Of Char) = toReplace.ToList()
chars.ForEach(AddressOf replaceVal)
Console.WriteLine(mainString)
Console.ReadLine()
End Sub
Dim mainString As String = "this is my string that has values in it that I am going to quickly replace all of..."
Sub replaceVal(ByVal c As Char)
mainString = mainString.Replace(c, "")
End Sub
可以这样做吗?
答案 0 :(得分:25)
如果我读得正确,你试图从字符串中删除一个字符列表。这非常适合RegEx。
Console.WriteLine(Regex.Replace("abcdefghijklmnop", "[acegi]", string.Empty))
(您需要导入System.Text.RegularExpressions)
答案 1 :(得分:6)
RegEx方法是最合适的,但我真正需要说的是:
请注意,对于维护开发人员的爱,不要因为将这一点归结为一行代码而陷入困境。一个方法调用是你真正的目标,如果你最终只是将一堆调用打成一行来说它是单行,那么你就是在自己的脚下射击。
答案 2 :(得分:6)
我不相信Bittercode,因为他说LINQ会胜过正则表达式。 所以我做了一点测试以确定。
如何执行此操作的三个示例:
Dim _invalidChars As Char() = New Char() {"j"c, "a"c, "n"c}
Dim _textToStrip As String = "The quick brown fox jumps over the lazy dog"
Private Sub btnStripInvalidCharsLINQ_Click(sender As System.Object, e As System.EventArgs) Handles btnStripInvalidCharsLINQ.Click
Dim stripped As String = String.Empty
Dim sw As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To 10000
stripped = _textToStrip.Where(Function(c As Char) Not _invalidChars.Contains(c)).ToArray
Next
sw.Stop()
lblStripInvalidCharsLINQ.Text = _stripped & " - in " & sw.Elapsed.TotalMilliseconds & " ms"
End Sub
Private Sub btnStripInvalidCharsFOR_Click(sender As System.Object, e As System.EventArgs) Handles btnStripInvalidCharsFOR.Click
Dim stripped As String = String.Empty
Dim sw As Stopwatch = Stopwatch.StartNew
stripped = _textToStrip
For i As Integer = 0 To 10000
For Each c As Char In _invalidChars
stripped = stripped.Replace(c, "")
Next
Next
sw.Stop()
lblStipInvalidcharsFor.Text = stripped & " - in " & sw.Elapsed.TotalMilliseconds & " ms"
End Sub
Private Sub btnStripInvalidCharsREGEX_Click(sender As System.Object, e As System.EventArgs) Handles btnStripInvalidCharsREGEX.Click
Dim stripped As String = String.Empty
Dim sw As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To 10000
stripped = Regex.Replace(_textToStrip, "[" & New String(_invalidChars) & "]", String.Empty)
Next
sw.Stop()
lblStripInvalidCharsRegex.Text = stripped & " - in " & sw.Elapsed.TotalMilliseconds & " ms"
End Sub
结果:
因此,使用string.replace的for循环优于所有其他方法。
因此我会对字符串对象进行扩展功能。
Module StringExtensions
<Extension()> _
Public Function ReplaceAll(ByVal InputValue As String, ByVal chars As Char(), replaceWith As Char) As String
Dim ret As String = InputValue
For Each c As Char In chars
ret = ret.Replace(c, replaceWith)
Next
Return ret
End Function
然后你可以在一行中使用这个函数,并且可以读取:
_textToStrip.ReplaceAll(_invalidChars, CChar(String.Empty))
答案 3 :(得分:1)
String类有一个替换方法来执行此操作。你可以这样使用它:
YourString = YourString.Replace("OldValue", "NewValue")
答案 4 :(得分:1)
我推荐使用Jon Galloway的方法,正则表达式是适当的方法,未来的开发人员会感谢你:) - 尽管使用Linq解决这个问题也不是一个难题。这是一些(未经测试的)C#代码:
string stringToBeReplaced = "abcdefghijklmnop";
string charsToReplace = "acegi";
stringToBeReplaced = new String(stringToBeReplaced.Where(c => !charsToReplace.Any(rc => c == rc)).ToArray());
我怀疑如果性能有问题,这段代码的性能可能会比正则表达式略好一些。
答案 5 :(得分:0)
如果您真的想要使用该代码,重复行语句的最佳方法是什么:
Sub Main()
Dim myString As String = Nothing
Dim finalString As String = Nothing
Console.Write("Please enter a string: ") 'your free to put anything
myString = Console.ReadLine()
finalString = myString.Replace("0", "")
myString = finalString
finalString = myString.Replace("1", "")
myString = finalString
finalString = myString.Replace("2", "")
myString = finalString
finalString = myString.Replace("3", "")
myString = finalString
finalString = myString.Replace("4", "")
myString = finalString
finalString = myString.Replace("5", "")
myString = finalString
finalString = myString.Replace("6", "")
myString = finalString
finalString = myString.Replace("7", "")
myString = finalString
finalString = myString.Replace("8", "")
myString = finalString
finalString = myString.Replace("9", "")
Console.WriteLine(finalString)
Console.ReadLine()
End Sub
例如:如果您输入:012ALP456HA90BET678
,则输出为:ALPHABET
。
答案 6 :(得分:0)
Public Function SuperReplace(ByRef field As String, ByVal ReplaceString As String) As String
' Size this as big as you need... it is zero-based by default'
Dim ReplaceArray(4) As String
'Fill each element with the character you need to replace'
ReplaceArray(0) = "WARD NUMBER "
ReplaceArray(1) = "WN "
ReplaceArray(2) = "WARD NO "
ReplaceArray(3) = "WARD-"
ReplaceArray(4) = "WARD "
Dim i As Integer
For i = LBound(ReplaceArray) To UBound(ReplaceArray)
field = Replace(field, ReplaceArray(i), ReplaceString)
Next i
SuperReplace = field
End Function
答案 7 :(得分:0)
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
Dim s As String = "México Juárez índice recúrso dirección"
Dim arr() As String = {"á", "é", "í", "ó", "ú", "Ñ", "ñ"}
Dim rep() As String = {"a", "e", "i", "o", "u", "N", "n"}
Dim i As Integer = Nothing
For i = 0 To UBound(arr)
s = Replace(s, arr(i), rep(i))
Next
MsgBox(s)
End Sub