Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. STRING MANIPULATION

STRING MANIPULATION

Scheduled Pinned Locked Moved C#
tutorial
12 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P POKRI

    HOW TO CHECK IN A TEXT BOX WHETHER THE INPUT VALUE IS STRING OR NUMERIC VALUE

    L Offline
    L Offline
    Larry J Siddens
    wrote on last edited by
    #2

    A down and dirty way is to convert the text to a number in a try-catch block. If an exception is thrown, then .... I'm trapping on the KeyPressed event and checking each key pressed. If it isn't the key I want, I disregard it. If anyone has a better way of doing this, I would like to see it. Larry J. Siddens

    1 Reply Last reply
    0
    • P POKRI

      HOW TO CHECK IN A TEXT BOX WHETHER THE INPUT VALUE IS STRING OR NUMERIC VALUE

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #3

      As the previous reply mentioned, you can do something like this:

      public bool IsNumeric(TextBox tb)
      {
      try
      {
      Int32.Parse(tb.Parse);
      }
      catch
      {
      return false;
      }
      }

      If you need to check larger values or decimal values, you could replace Int32 with Int64 (long) or Double (double), respectively. If you want to allow other characters like a group separator ("." or ",", depending on the current culture), currenty symbols, etc., use the Int32.Parse(string, NumberStyles) overload and see the documentation for the NumberStyles enum for more information. Also, in the future please do not use all caps. Most times, it's considered flaming (yelling at someone) - which most likely you didn't intend - but it still makes it hard to read.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      L 1 Reply Last reply
      0
      • P POKRI

        HOW TO CHECK IN A TEXT BOX WHETHER THE INPUT VALUE IS STRING OR NUMERIC VALUE

        J Offline
        J Offline
        Jose Fco Bonnin
        wrote on last edited by
        #4

        What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }

        H L P 3 Replies Last reply
        0
        • J Jose Fco Bonnin

          What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #5

          Sure, but keep in mind that regular expressions are inherently slower than simple string parsing. If you will be using this regex several times, you should at least compile it. This will boost performance a bit, but it will still be slower than just parsing a string, especially in such an easy case.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          1 Reply Last reply
          0
          • H Heath Stewart

            As the previous reply mentioned, you can do something like this:

            public bool IsNumeric(TextBox tb)
            {
            try
            {
            Int32.Parse(tb.Parse);
            }
            catch
            {
            return false;
            }
            }

            If you need to check larger values or decimal values, you could replace Int32 with Int64 (long) or Double (double), respectively. If you want to allow other characters like a group separator ("." or ",", depending on the current culture), currenty symbols, etc., use the Int32.Parse(string, NumberStyles) overload and see the documentation for the NumberStyles enum for more information. Also, in the future please do not use all caps. Most times, it's considered flaming (yelling at someone) - which most likely you didn't intend - but it still makes it hard to read.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            L Offline
            L Offline
            Larry J Siddens
            wrote on last edited by
            #6

            Heath, How you doing? Have you used the method "TryParse"? I see that as a method in the System.Double class. Larry J. Siddens

            H 1 Reply Last reply
            0
            • L Larry J Siddens

              Heath, How you doing? Have you used the method "TryParse"? I see that as a method in the System.Double class. Larry J. Siddens

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #7

              If you read the documentation, this is just like Parse but it try/catches internally and returns a boolean if the value could be successfully parsed. If it could, the out param contains the value, something similar to this:

              public static bool TryParse(string s, NumberStyles style,
              IFormatProvider provider, out double result)
              {
              try
              {
              result = double.Parse(s, style, provider);
              }
              catch
              {
              return false;
              }
              }

              It just depends on whichever you want to use. If you don't want to handle exceptions yourself, use Double.TryParse.

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              L 1 Reply Last reply
              0
              • H Heath Stewart

                If you read the documentation, this is just like Parse but it try/catches internally and returns a boolean if the value could be successfully parsed. If it could, the out param contains the value, something similar to this:

                public static bool TryParse(string s, NumberStyles style,
                IFormatProvider provider, out double result)
                {
                try
                {
                result = double.Parse(s, style, provider);
                }
                catch
                {
                return false;
                }
                }

                It just depends on whichever you want to use. If you don't want to handle exceptions yourself, use Double.TryParse.

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                L Offline
                L Offline
                Larry J Siddens
                wrote on last edited by
                #8

                Thanks for the reply Heath! Larry J. Siddens

                1 Reply Last reply
                0
                • J Jose Fco Bonnin

                  What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #9

                  Jose Fco Bonnin wrote: @"0*[1-9][0-9]*" WTF? Firstly, why not use @"\d+" ? And what is the purpose of the "prepended" zero's? :confused: And lastly, why test it again (not correctly even!) ? Very counter-productive... leppie::AllocCPArticle("Zee blog");
                  Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                  J 1 Reply Last reply
                  0
                  • J Jose Fco Bonnin

                    What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }

                    P Offline
                    P Offline
                    POKRI
                    wrote on last edited by
                    #10

                    Thank you Bonnin, it works fine. can u explain the whole stuff, ie why we want 0,*....... all i changed \p{Nd} instead of [0-9] and it works fine since i use unicode. those who reponded to my query once again, thanks a lot.

                    1 Reply Last reply
                    0
                    • L leppie

                      Jose Fco Bonnin wrote: @"0*[1-9][0-9]*" WTF? Firstly, why not use @"\d+" ? And what is the purpose of the "prepended" zero's? :confused: And lastly, why test it again (not correctly even!) ? Very counter-productive... leppie::AllocCPArticle("Zee blog");
                      Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                      J Offline
                      J Offline
                      Jose Fco Bonnin
                      wrote on last edited by
                      #11

                      if you use @"\d+" and your string is "aaaa1" the result will be true.

                      L 1 Reply Last reply
                      0
                      • J Jose Fco Bonnin

                        if you use @"\d+" and your string is "aaaa1" the result will be true.

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #12

                        Oops, just forgot the line markers, should be @"^\d+$". :eek: leppie::AllocCPArticle("Zee blog");
                        Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups