• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Check the String against Special Characters

Hello,

I have a string and I want to check this string whether it contains any Special characters and spaces. If it contains any, I want to dispaly it as a label text.
How can I do this?
0
praveenuni
Asked:
praveenuni
  • 4
  • 3
  • 2
2 Solutions
 
YurichCommented:
There are several ways, but I'd prefer using regular expressions, for example:

using System.Text.RegularExpressions;
...

// somewhere where you want to do checking
Regex myRegex = new Regex( "[^A-Za-z0-9.,]" );

string s = GetStringWithSpecialCharacters();
MatchCollection matches = myRegex.Matches( s );

string t = "";
foreach( Match m in matches )
      t += m.ToString() + " ";
label1.Text = t;

the code above will filter out all letters from a-z and A-Z as well as numbers from 0-9 and characters '.' and ','. You can of course modify the Regex string as it would be more suitable for you.

you can use this link as a guide
http://www.regular-expressions.info/

the same as above can be of course implemented using chars and strings but using regex is more professional ;)

regards

0
 
nouloukCommented:
You can use foreach statement:

foreach (Char c in mystring)
{
      if (c==char(32))
           {
               Console.WriteLine("Char is Space");
               break;
           }
}

Something like this.Hope this helps.
0
 
praveenuniAuthor Commented:
I want to filter . (dot) and ,(Comma) also in the string. I'm pretty new to this regular expressions. Could you please tell me on how to do this?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nouloukCommented:
Regex myRegex = new Regex( "[^A-Za-z0-9]" );  // delete dot and comma in this line.
0
 
praveenuniAuthor Commented:
I want to know if there any invalid characters in the string. But the above code is not checking for any IF. It is just writing the invalid values to the label. I just want to know if there are any invalid characters. How should I modify the code?
0
 
nouloukCommented:
0
 
nouloukCommented:
I think you can do like this (not tested):
using System.Text.RegularExpressions;

Regex myRegex = new Regex( "[^A-Za-z0-9]" );

string s = GetStringWithSpecialCharacters();
MatchCollection matches = myRegex.Matches( s );

string t = "mystring";
foreach( Match m in matches )
     {
label1.Text = t;
break;
     }
0
 
YurichCommented:
2 noulouk,
how is it different from my code??

to check whether there any invalid characters use the following:

Regex myRegex = new Regex( "[^A-Za-z0-9]" );

string s = GetStringWithSpecialCharacters();
bool status = myRegex.IsMatch( s );

if( status )
{
    // here is your code if you have any special characters
}

regards
0
 
praveenuniAuthor Commented:
Thank you Yurich and Noulok. It helped. In my code I did this ..

                        Regex myRegex = new Regex( "[^A-Za-z0-9]" );                  
                        MatchCollection matches = myRegex.Matches( StringtoCheck );
                        if (matches.Count > 0 )
                        {
                              ToGo = 1;
                        }

-- Praveen
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now