Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3432866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:30:10+00:00 2026-05-18T07:30:10+00:00

How fast can I replace characters in a string? So the background on this

  • 0

How fast can I replace characters in a string?

So the background on this question is this: We have a couple of applications that communicate with each other and with clients’ applications through sockets. These socket messages contain non-printable characters (e.g. chr(0)) which need to get replaced with a predetermined string (e.g “{Nul}”}, because the socket messages are kept in a log file. On a side note, not every log message will need to have characters replaced.

Now I started off on this little adventure reading from this MSDN link which I found from a different post from this site.

The current method we used…at the beginning of the day…was using StringBuilder to check for all the 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

Now as the blog post points out leaving StringBuilder out and using string.replace does yield faster results. (Actually, using StringBuilder was the slowest method of doing this all day long.)

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

Knowing that not every message would need to go through this process I thought it would save time to not have to process those messages that could be left out. So using regular expressions I first searched the string and then determined if it needed to be processed or not. This was about the same as using the string.replace, basically a wash from saving the time of not processing all the strings, but losing time from checking them all with regular expressions.

Then it was suggested to try using some arrays that matched up their indexes with the old and the new and use that to process the messages. So it would be something like this…

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

This so far has been the fastest way I have found to process these messages. I have tried various other ways of going about this as well like converting the incoming string into a character array and comparing along with also trying to loop through the string rather than the chrArray.

So my question to all is: Can I make this faster yet? What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:30:11+00:00Added an answer on May 18, 2026 at 7:30 am

    You might be able to squeeze out a little more speed by reducing some lookups. Take for example this:

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

    The .Contains method is O(n). In the worst case, you’re going to traverse all the chars in the entire string without finding anything, so you expect to traverse at least one time for each character in your array, so its O(nm) where n is the length of your string and m is the number of chars you’re replacing.

    You might get a little better performance doing the following (my VB-fu is rusty, has not been tested 😉 ):

    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
    

    This will traverse chars in p_Message at most twice (once for traversing, once when the string constructor copies the char array), making this function O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.