Share via


Is there a way to check for a null characters in a string using Regex match

Question

Thursday, June 20, 2013 11:46 AM

I need to return false if null is present in string using regex.match

All replies (4)

Tuesday, June 25, 2013 11:26 AM âś…Answered

Are you looking for something like this:

string test = "MSDN Forums \00"; 
string pattern = "\x00";

Console.WriteLine("String {0} contains 0 is {1}.", test, Regex.IsMatch(test, pattern));

wizend


Thursday, June 20, 2013 12:04 PM

You need to clarifiy this:

NULL, 0 (the integer or float value) and '0' (the Char value) are three totally distinct things. And then there is also the "Null" (a string).

A string cannot contain NULL or 0. It can contain '0' (the char), "Null" (the string) or the reference to a string can be NULL. When a reference varriable is set to NULL it means "This varriable could point to an isntance of this class. But right it is not pointing to anything. There is nothing to see here".

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


Thursday, June 20, 2013 12:05 PM

^(?!\s*$).+

will match any string that contains at least one non-space character.
Example:

if(Regex.IsMatch(subjectString, @"^(?!\s*$).+")) 
{
     // Successful match
} 
else
{ 
   // Match attempt failed
}

--

But if you only need to check for null (or at least this is one of the conditions you can simply check for null in the 1st place:

string str = //...
if(str != null)
{
   //continue, not nulll
}
else
{
   //its null
}

Mitja


Thursday, June 20, 2013 12:32 PM

Thanks for replies,but i need to check '\0' character in a string using regex.match is this possible any how can check using

string str="MSDN Forums \0";

str.contains(char.Minvalue);

but is it possible thru regex?