substring numeric value of a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arial
    New Member
    • Sep 2007
    • 109

    substring numeric value of a string

    Hi all,

    I need some help from you expert. I need to extract numeric value of a string.

    for ext: string str = "1234 absc st" or it can be "123 hello world"
    or "1234567 asp.net"


    How can i only get the numeric part of the string?

    Please help and give me some suggestions.

    Thank you,
  • Kapps
    New Member
    • Aug 2008
    • 16

    #2
    One potential way would be to go through each char in the string, and check if it's numeric. If it is, add it on to a temporary string, which is then returned. I'm not sure how efficient this is however.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Regular expressions would probably be useful for this.

      I'm not good with RegEx myself, so I can't give you a sample, but maybe someone else can help with that.

      Comment

      • chelvan
        New Member
        • Aug 2008
        • 90

        #4
        hi

        you always find a space after your numeric value on your string. so its simple, first you find the position of the space , then you able to copy your numeric value by using substring.

        regards
        chel-1

        Comment

        • artov
          New Member
          • Jul 2008
          • 40

          #5
          If the number is at the first, you might like to use Split method using space
          as separator, the Parse (or better yet, TryParse) to get the value.

          Comment

          • chelvan
            New Member
            • Aug 2008
            • 90

            #6
            split your string into an string array by space position. then convert your array element into integer. exception will throws when you cast the string. only one array element never throws exception thats element holds your answer.

            chel-1

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Code:
              string MyAddress = "123 N. Main Street";
              int HouseNumber = Convert.ToInt32(MyAddress.Substring(0, MyAddress.IndexOf(' ')));
              MessageBox.Show(HouseNumber.ToString(), "House number");
              Give this a try.
              Its up to you to beef it up, give it some value qualification and error handling. But should point you in a workable direction.

              Comment

              • chelvan
                New Member
                • Aug 2008
                • 90

                #8
                Originally posted by tlhintoq
                Code:
                string MyAddress = "123 N. Main Street";
                int HouseNumber = Convert.ToInt32(MyAddress.Substring(0, MyAddress.IndexOf(' ')));
                MessageBox.Show(HouseNumber.ToString(), "House number");
                Give this a try.
                Its up to you to beef it up, give it some value qualification and error handling. But should point you in a workable direction.

                hi its right.
                but my question is whats your idea
                when the string value turn to like this "abc 14526 kio mk";

                chel-1

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Have a look at

                  Code:
                  string s = "56ttt777";
                  foreach(string strItem in Regex.Split(s,"\\D")){
                  	Console.WriteLine(strItem);
                  }
                  You'd need use System.Text.Reg ularExpressions of course.

                  Comment

                  • arial
                    New Member
                    • Sep 2007
                    • 109

                    #10
                    Thank you all for all your suggetion.

                    like chel-1 said i need to make sure it works both way,
                    123 abc and abnd 345 edf.

                    I will give a try to your suggetion and let you all know what happens.

                    Thank You,

                    Comment

                    • arial
                      New Member
                      • Sep 2007
                      • 109

                      #11
                      Hi all,

                      Back to my problem.

                      I will to save numeric value in separate string as well as the characters.

                      like,,,,,12345 abcdefh

                      I need string1 = "12345" and string2 = "abcdefh"

                      Any idea how can i do this?

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Originally posted by arial
                        Hi all,

                        Back to my problem.

                        I will to save numeric value in separate string as well as the characters.

                        like,,,,,12345 abcdefh

                        I need string1 = "12345" and string2 = "abcdefh"

                        Any idea how can i do this?
                        [code=c#]
                        //asuming always divided by a space
                        string startstring="12 345 abcdefh";
                        string[] pieces startstring.Spl it(' ');
                        pieces[0]="12345";//can be parsed by int.Parse() now
                        pieces[1]= "abcdefh";
                        //note there could be a pieces[3]and etc if there are more spaces
                        [/code]

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by arial
                          Hi all,

                          Back to my problem.

                          I will to save numeric value in separate string as well as the characters.

                          like,,,,,12345 abcdefh

                          I need string1 = "12345" and string2 = "abcdefh"

                          Any idea how can i do this?
                          Did you read/try my post above? Regex is the proper way of doing these things.

                          Comment

                          • arial
                            New Member
                            • Sep 2007
                            • 109

                            #14
                            Thank you all for help. My problem is solved.

                            Thank you,

                            Comment

                            Working...