Replacing characters in strings in VB.NET

How quickly can characters be replaced in a string?

So, this question is asked: we have several applications that communicate with each other and with client applications through sockets. These socket messages contain non-printable characters (for example, chr (0)) that need to be replaced with the specified string (for example, "{Nul}"}, because the socket messages are stored in a log file. Each log message will need to be replaced with characters.

Now I started with this little adventure reading from this MSDN link that I found from another post from this site.

The current method we used ... at the beginning of the day ... used StringBuilder to check for all possible replacements, such as ...

    Public Function ReplaceSB(ByVal p_Message As String) As String
      Dim sb As New System.Text.StringBuilder(p_Message)

      sb.Replace(Chr(0), "{NUL}")
      sb.Replace(Chr(1), "{SOH}")

      Return sb.ToString
    End Function

, , StringBuilder, string.replace . (, StringBuilder .)

    p_Message = p_Message.Replace(Chr(0), "{NUL}")
    p_Message = p_Message.Replace(Chr(1), "{SOH}")

, , , , , . , , , , . , string.replace, , , , .

, , . - ...

Private chrArray() As Char = {Chr(0), Chr(1)}
Private strArray() As String = {"{NUL}", "{SOH}"}

Public Function TestReplace(ByVal p_Message As String) As String
    Dim i As Integer

    For i = 0 To ((chrArray.Length) - 1)
        If p_Message.Contains(chrArray(i).ToString) Then
            p_Message = p_Message.Replace(chrArray(i), strArray(i))
        End If
    Next

    Return p_Message
End Function

, . , , chrArray.

, : ? ?

+5
5

, , . , , :

    If p_Message.Contains(chrArray(i).ToString) Then

.Contains - O (n). , , , O (nm), n - m - , .

, ( VB-fu , ;)):

Private Function WriteToCharList(s as String, dest as List(Of Char))
    for each c as Char in s
        dest.Add(c)
    Next
End Function

Public Function TestReplace(ByVal p_Message As String) As String
    Dim chars as new List(Of Char)(p_Message.Length)

    For each c as Char in p_Message
        Select Case c
            Case Chr(0): WriteToCharList("{NUL}", chars)
            Case Chr(1): WriteToCharList("{SOH}", chars)
            Case Else: chars.Add(c);
        End Select
    Next

    Return New String(chars)
End Function

p_Message ( , , char), O (n).

+1

StringBuilder Replace() .NET.

0

:

  • , , .IndexOf() .Contains(), .
  • , , StringBuilder , .ToString() - (: .ToString() , )
  • , , / , StringReader/TextReader , .

, :

Public Function TestReplace(ByVal p_Message As String) As String
    Static chrArray() As Char = {ChrW(0), ChrW(1)}
    Static strArray() As String = {"{NUL}", "{SOH}"}

    Dim rdr As New StringReader(p_Message)
    Dim result As New StringWriter()

    Dim i As Integer
    While (i = rdr.Read()) <> -1
        Dim c As Char = ChrW(i)
        Dim index As Integer = Array.IndexOf(chrArray, c)
        If index >= 0 Then result.Write(strArray(index)) Else result.Write(c)
    End While

    Return result.ToString()
End Function

Please note that your tests will largely depend on the type of strings you throw at it, so make sure you use the most representative sample (and this should be a good sized sample).

0
source

Take a look at this example . It has some comparative statistics comparing two methods.

0
source

It should also be faster:

    Private Shared strList As New Dictionary(Of Char, String)

    Shared Sub New()
        strList.Add(Chr(0), "{NUL}")
        strList.Add(Chr(1), "{SOH}")
    End Sub

    Public Function TestReplace(ByVal p_Message As String) As String
        For Each c As Char In strList.Keys
            If p_Message.IndexOf(c) <> -1 Then
                p_Message = p_Message.Replace(c, strList(c))
            End If
        Next

        Return p_Message
    End Function
0
source

All Articles