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.
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.
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]", " ")
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 :
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?
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.
Member
32 Points
203 Posts
Replace Control Characters in a String
Jul 03, 2013 03:29 PM|rspiet|LINK
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.
All-Star
28988 Points
7251 Posts
Re: Replace Control Characters in a String
Jul 03, 2013 03:39 PM|Rajneesh Verma|LINK
Use String.Replace,
Have a look
http://www.dotnetperls.com/replace
www.rajneeshverma.com
All-Star
35149 Points
9075 Posts
Re: Replace Control Characters in a String
Jul 03, 2013 03:40 PM|smirnov|LINK
The question is not very clear, I suppose something like this should work
If you need to replace multiple symbols than I think you can do
Member
32 Points
203 Posts
Regular Expression to Replace Control Characters with Spaces In a String
Jul 05, 2013 02:38 PM|rspiet|LINK
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.
Member
32 Points
203 Posts
Re: Regular Expression to Replace Control Characters with Spaces In a String
Jul 05, 2013 03:22 PM|rspiet|LINK
I think I found my own answer. I used Regex.Replace("
Regex.Replace(InputString, "[\x00-\x08\x0B-\x0C\x0E-\x1F]")
Member
32 Points
203 Posts
Most Efficient Way to Sanitize a String
Jul 08, 2013 12:23 PM|rspiet|LINK
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]", " ")
All-Star
114593 Points
18503 Posts
MVP
Re: Most Efficient Way to Sanitize a String
Jul 08, 2013 12:45 PM|Rion Williams|LINK
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 :
So you could easily use it through :
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?
Member
32 Points
203 Posts
Re: Most Efficient Way to Sanitize a String
Jul 08, 2013 02:08 PM|rspiet|LINK
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.
Star
14544 Points
1481 Posts
Re: Replace Control Characters in a String
Jul 09, 2013 02:43 AM|Amy Peng - MSFT|LINK
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