locked
Replace Control Characters in a String RRS feed

  • Question

  • User2023521679 posted

    Can someone please show me a function like Function ReplaceControlCharacters(InputString As String, ReplacementString As String) As String where it replaces all control characters like 0x07 with a ReplacementString. I know there has to be a way to do this with Regular Expressions, I am just not sure of the syntax for this.

    Friday, July 5, 2013 2:38 PM

Answers

  • User281315223 posted

    I'm not sure regarding the exact format of your characters or string, however the following Regular Expression should be sufficient to match any hexidecimal characters : 

    0x[a-fA-F\d]{2}

    So you could easily use it through : 

    OutputString = Regex.Replace(InputString, "0x[a-fA-F\d]{2}", " ")

    A Regular Expression should be fast enough for your needs since you are dealing with a pattern. If that was not the case, you might consider using the String.Replace() method (but that would only be faster for static strings).

    Do you have an extended example of how you are currently using this?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 8, 2013 12:45 PM
  • User260886948 posted

    Hi,

    I think the following should help you:

    a=a.Replace("0x07","the string you wan to");

    Here is a similar thread, please try to refer to:

    http://stackoverflow.com/questions/10489775/replace-control-character-in-string-c-sharp .

    Hope it can help you.

    Best Regards,
    Amy Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 9, 2013 2:43 AM

All replies

  • User1992938117 posted

    I am just not sure of the syntax for this.

    Use String.Replace,

    Have a look

    http://www.dotnetperls.com/replace

    Wednesday, July 3, 2013 3:39 PM
  • User1508394307 posted

    The question is not very clear, I suppose something like this should work

    byte b = 0x07;
    string result = input.Replace(b.ToString(), "");

    If you need to replace multiple symbols than I think you can do

    byte[] bb = {0x01, 0x02, ...}
    foreach (byte b in bb)
    ...
    Wednesday, July 3, 2013 3:40 PM
  • User2023521679 posted

    I am trying to come up with a regular expression that takes a string and replaces and control characters with spaces to avoid messages like hexadecimal value 0x07, is an invalid character.

    The control characters are &H0 - &H8, &H11, &H12, &H14 - &H31. Tabs, Carraige Returns, and Line Feeds are okay.

    Friday, July 5, 2013 2:38 PM
  • User2023521679 posted

    I think I found my own answer. I used Regex.Replace("

    Regex.Replace(InputString, "[\x00-\x08\x0B-\x0C\x0E-\x1F]")

    Friday, July 5, 2013 3:22 PM
  • User2023521679 posted

    I recently discovered a problem creating a SpreadsheetML document because there were invalid hexadecimal control characters in my input database (e.g., hexadecimal value 0x07, is an invalid character). I wrote a Regex to replace these types of characters to spaces, but it is really slow when I am writing out a lot of data which is usually the case. What is the most efficient way to do this?

    My method, which works is to use OutputString = Regex.Replace(InputString, "[\x00-\x08\x0B-\x0C\x0E-\x1F]", " ")

     

    Monday, July 8, 2013 12:23 PM
  • User281315223 posted

    I'm not sure regarding the exact format of your characters or string, however the following Regular Expression should be sufficient to match any hexidecimal characters : 

    0x[a-fA-F\d]{2}

    So you could easily use it through : 

    OutputString = Regex.Replace(InputString, "0x[a-fA-F\d]{2}", " ")

    A Regular Expression should be fast enough for your needs since you are dealing with a pattern. If that was not the case, you might consider using the String.Replace() method (but that would only be faster for static strings).

    Do you have an extended example of how you are currently using this?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 8, 2013 12:45 PM
  • User2023521679 posted

    Wouldn't that replace all characters with a space, control characters or not. I'm just trying to replace 0x00 through 0x08, 0xB0, 0xC0, 0xE0 through 0x1F.

    Monday, July 8, 2013 2:08 PM
  • User260886948 posted

    Hi,

    I think the following should help you:

    a=a.Replace("0x07","the string you wan to");

    Here is a similar thread, please try to refer to:

    http://stackoverflow.com/questions/10489775/replace-control-character-in-string-c-sharp .

    Hope it can help you.

    Best Regards,
    Amy Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 9, 2013 2:43 AM