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.
, : ? ?