Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't believe Bittercode as he said that LINQ would outperform regex. So I did a little test just to be sure.</p> <p>Three examples of how to do this:</p> <pre><code>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 &amp; " - in " &amp; sw.Elapsed.TotalMilliseconds &amp; " 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 &amp; " - in " &amp; sw.Elapsed.TotalMilliseconds &amp; " 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, "[" &amp; New String(_invalidChars) &amp; "]", String.Empty) Next sw.Stop() lblStripInvalidCharsRegex.Text = stripped &amp; " - in " &amp; sw.Elapsed.TotalMilliseconds &amp; " ms" End Sub </code></pre> <p>The results:</p> <p><img src="https://i.stack.imgur.com/SqUvd.png" alt="Performance results"> <br></p> <p>So, the for loop with string.replace outperformes all the other methods.</p> <p>Because of this I would make an extension function to the string object. </p> <pre><code>Module StringExtensions &lt;Extension()&gt; _ Public Function ReplaceAll(ByVal InputValue As String, ByVal chars As Char(), replaceWith As Char) As String Dim ret As String = String.Empty For Each c As Char In chars ret.Replace(c, replaceWith) Next Return ret End Function </code></pre> <p>Then you could use this function nice and readably in one line:</p> <pre><code>_textToStrip.ReplaceAll(_invalidChars, CChar(String.Empty)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload