none
How to check a string for special characters if it also contain spaces

    Question

  • hi,
     I am trying to check whether a string(talen as input from user) contains special characters or not and if the string also have some whitespaces ,what to do? Please give me a code.
    Wednesday, October 28, 2009 4:53 AM

Answers

  • Hi,
    I hope it will help you.
     char[] str = textBox1.Text.Trim().ToCharArray();
                foreach (char c in str)
                {
                    if(char.IsDigit(c))
                    {
                        MessageBox.Show("No="+c.ToString());
                    }
                    else if(char.IsSymbol(c))
                    {
                        MessageBox.Show("Symbol="+c.ToString());
                    }
                    else if(char.IsWhiteSpace(c))
                    {
                        MessageBox.Show("White Space"+c.ToString());
                    }
    
                }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Marked as answer by Sharique Ali Wednesday, October 28, 2009 8:31 AM
    Wednesday, October 28, 2009 6:16 AM

All replies

  • Strinx x="Your Str#ing";
    int chk=x.IndexOf('#');

    //You can see chk=8;

    Strinx x="Your Str#ing";
    int chk=x.IndexOf('W');


    //You can see chk=-1;

    Wednesday, October 28, 2009 5:14 AM
  • Hi,
    I hope it will help you.
     char[] str = textBox1.Text.Trim().ToCharArray();
                foreach (char c in str)
                {
                    if(char.IsDigit(c))
                    {
                        MessageBox.Show("No="+c.ToString());
                    }
                    else if(char.IsSymbol(c))
                    {
                        MessageBox.Show("Symbol="+c.ToString());
                    }
                    else if(char.IsWhiteSpace(c))
                    {
                        MessageBox.Show("White Space"+c.ToString());
                    }
    
                }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Marked as answer by Sharique Ali Wednesday, October 28, 2009 8:31 AM
    Wednesday, October 28, 2009 6:16 AM
  • your given code's first line is very useful thanx
    Wednesday, October 28, 2009 8:31 AM