Results 1 to 18 of 18

Thread: Check for characters

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Check for characters

    This causes a fatal error if the input is not a number.

    if ((float.Parse(x.Text) >- 1)||(float.Parse(x.Text) < 9999999))

    How do I stop the the conversion if the user enters in a character?

    How do I check to see if what was entered was a character or number?

  2. #2
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    Throw a textBox on a form called textBox1 and give this a try.
    PHP Code:
    string outputStr "";
                foreach(
    char myChar in textBox1.Text)
                {
                    if(
    Char.IsNumber(myChar))
                    {
                        
    outputStr += myChar.ToString();
                    }
                }
                
    textBox1.Text outputStr;
                
    textBox1.Select(textBox1.Text.LengthtextBox1.Text.Length +1); 
    Jeremy

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Put the above in a try catch block.
    Dont gain the world and lose your soul

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Just off the top of my head, wouldn't that cause a problem if the user entered a decimal number?
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I'll have to give that a try and experimentation to see how it works.

  6. #6

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I Like that!!! It is something I was looking for anyways.

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I figured out a way around the . bug:

    if(!Char.IsSymbol(myChar)&&(!Char.IsLetter(myChar)))

  8. #8

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    But I can't get rid of the spaces. Any ideas?

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    PHP Code:
    string outputStr "";
    bool bFoundDecimal false;

                foreach(
    char myChar in textBox1.Text)
                {
                    if(
    Char.IsNumber(myChar))
                    {
                        
    outputStr += myChar.ToString();
                    }
                    else if ((!
    bFoundDecimal) && (myChar == '.'))
                    {
                        
    // there can only be one decimal!
                        
    outputStr += myChar.ToString();
                        
    bFoundDecimal true;
                    }
                }
                
    textBox1.Text outputStr;
                
    textBox1.Select(textBox1.Text.LengthtextBox1.Text.Length +1); 
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  10. #10

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    So I can substitute this

    (myChar == '.')

    with this

    (myChar == ' ')

    to get rid of the spaces and I won't forget to omit the bool for that.

    Right?

  11. #11
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    That should work.

    Jeremy

  12. #12

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Good! Will that work even if the user uses tabs instead of spaces. Or are tabs==spaces.

  13. #13
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    i do not think that tabs are spaces think that tabs are '\t' characters.

    Jeremy

  14. #14

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    You may be right. But I hope that tabs are evaluated as one space at a time.

  15. #15
    Junior Member
    Join Date
    Nov 2002
    Posts
    20
    In C#, how to determine if the string is a number or not?

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What kind of number?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Junior Member
    Join Date
    Nov 2002
    Posts
    20
    any.
    i'm writting a program which highlight source code. because i want to check if the string is a number to mark it.
    some examples:
    3
    3.12

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What about hexadecimal?
    Code:
    bool isNumber(string str)
    {
      bool ret = str.Length > 0;
      int hasPoint = 0;
      foreach(char c in str) {
        ret = ret && (char.IsDigit(c) || (c == '.' && hasPoint++ == 0));
      }
      return ret;
    }
    Note that this will fail if your string contains more than 2^32 '.'
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width