Checking for ilegal symbols in a string.

Hi!

I want check a string looking for characteres that aren't letters from A to Z or numbers from 0 to 9. I can do it with multiple string.Contains(character) but isn't a good way.

Some way mor efficient and fast

Regards.



Answer this question

Checking for ilegal symbols in a string.

  • Arnaud H

    A lot of thanks.

    How add spaces in this pattern I forget that are necesaries too :P

    "[^A-Z0-9' ']"

    Regards.


  • Anu Viswan

    Hi,

    you can use regular expressions for this kind of checking. The following code sets resultado to true, if string Texto doesn't include any characters that don't fall in the A-Z 0-9 range (that includes spaces):

    string pattern = "[^A-Z0-9]";
    bool resultado = !Regex.IsMatch(Texto, pattern, RegexOptions.IgnoreCase);

    Hope it helps,

    Andrej



  • ravindra_pn

    Thanks again. :)

    Regards.


  • Eva Gonzalez

    Just insert a space in the pattern (see below, after number 9):

    [^A-Z0-9 ]

    Andrej



  • Expressman

    Is this correct

    Resultado=true;
    Texto.ToUpper();
    foreach (char CH in Texto)
    {
    if ((CH > 90) || (CH < 65)) Resultado = false;
    if ((CH > 57) || (CH < 48)) Resultado = false;
    }
    if(Resultado) MessageBox.Show("Valid string");


    Regards.


  • Checking for ilegal symbols in a string.