Here is one method to manipulate Null characters in C#:
To Search a String for a Null character:
mystring.Contains(Convert.ToChar(0x0).ToString() ); //The hexidecimal 0x0 is the null character
To Replace all Null Characters in a String:
mystring.Replace(Convert.ToChar(0x0).ToString(), "");
Why Do I Need This?
It might help. I needed it when I got a nasty error message in a data layer that was using asp.net web services and soap to retrieve the data:
“There is an error in XML document hexadecimal value 0x00, is an invalid character”
Basically what happened is that a null character was pasted into a SQL Server database via an Access front end. When my asp.net app was pulling the data via a SOAP data layer, the null character can not be used in a XML document with UTF encoding. Which is the transport method of this particualr service. So the quick hack was to strip out the null character after the data pull but before the soap transfer back to the client.
December 21st, 2009 at 6:23 am
Grt work dude it helped me alot…thanx
November 26th, 2010 at 2:21 pm
I need a conversion of Constants.vbNullChar to a C# method. I’ve tried creating a constant like this:
private const String vbNullChar = Convert.ToChar(0x0);
but that gives me an error. Any ideas on how to do this?
Thanks
November 26th, 2010 at 2:29 pm
… nevermind, I figured it out.. just don’t make it a constant but just declare it like this:
private String vbNullChar = Convert.ToChar(0).ToString();
February 15th, 2011 at 5:41 pm
@George You can also use Microsoft.VisualBasic.Constants.vbNullChar (from Microsoft.VisualBasic.dll).
March 11th, 2011 at 1:41 am
Thanks a ton! much appreciated
December 30th, 2011 at 2:48 pm
Yo, that\’s what\’s up turthlfuly.
August 20th, 2011 at 8:39 pm
(char)0 achieves much the same thing
October 7th, 2011 at 1:05 pm
Thank you very much. It solved the problem we had reading a html files saved as a binary field in Oracle.
December 30th, 2011 at 9:02 am
A little rationality lifts the quality of the debate here. Thanks for cotnribunitg!
October 8th, 2011 at 2:32 am
Char a=Convert.ToChar(0x0) is the correct.
thank you, It helps me.
December 28th, 2011 at 8:03 am
Thanks a lot for sharing this with all people you really recognize what you’re talking about! Bookmarked. Please also visit my site =). We will have a hyperlink trade contract between us
December 30th, 2011 at 11:03 am
A little rationality lifts the quality of the debate here. Thanks for corntibtiung!
July 16th, 2012 at 7:14 am
” is a character literal for the null character.
July 16th, 2012 at 7:15 am
backslash (\) + zero (0). It seems it was stripped out from the comment.
February 28th, 2013 at 5:58 pm
It’s weird in my case that Replace(”, ‘ ‘) doesn’t work for me. But Trim(”) works find. Luckily in my case there is no null characters in the middle. (My project is a WCF in .NET4, VS2010)