Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace multiple characters in string in one line of code in VB.NET

Using VB.NET I'd like to be able to replace a range of characters in a string in a single line of code.

I.e., something like:

Dim charsToReplace as string = "acegi"
Dim stringToBeReplaced as string = "abcdefghijklmnop"

charsToReplace.ToArray().ForEach(Function (c) stringTobeReplaced = stringTobeReplaced.Replace(c, ""))

However, this doesn't work.

The following does work, however I don't want the string to be a class level variable:

 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

Can this be done?

like image 697
hitch Avatar asked Aug 26 '09 05:08

hitch


People also ask

How to remove a substring from a string in VB net?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement.


2 Answers

I didn't believe Bittercode as he said that LINQ would outperform regex. So I did a little test just to be sure.

Three examples of how to do this:

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

The results:

Performance results

So, the for loop with string.replace outperformes all the other methods.

Because of this I would make an extension function to the string object.

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

Then you could use this function nice and readably in one line:

_textToStrip.ReplaceAll(_invalidChars, CChar(String.Empty))
like image 178
JDC Avatar answered Sep 25 '22 14:09

JDC


If I read this correctly, you're trying to strip a list of characters from a string. This is a good fit for a RegEx.

Console.WriteLine(Regex.Replace("abcdefghijklmnop", "[acegi]", string.Empty))

(you'll need to import System.Text.RegularExpressions)

like image 23
Jon Galloway Avatar answered Sep 26 '22 14:09

Jon Galloway