none
get digit RRS feed

  • Question

  •  string s1 = "xxdd_rfr_20160123_155_44";

                string s2 = "ddpp_20161123_1254_44";

    How to get these digits
    Thursday, December 29, 2016 1:32 PM

All replies

  • You mean extract each individual set of numbers?

    You could use a regular expression, as demonstrated in this console app:

    class Program
    {
    
    
            static void Main(string[] args)
            {
      
                string s1 = "xxdd_rfr_20160123_155_44";
                string s2 = "ddpp_20161123_1254_44";
    
                Regex expr = new Regex(@"\d+");
                TestString(expr, s1);
                TestString(expr, s2);
    
                Console.ReadKey();
            }
    
            public static void TestString(Regex expr, string str)
            {
                foreach (var match in expr.Matches(str))
                {
                    Console.WriteLine(match.ToString());
                }
            }
    }

    The output of this is:

    20160123
    155
    44
    20161123
    1254
    44

    Thursday, December 29, 2016 3:27 PM
  • I also suggest you use the following method

     Func<string, string> getIntPart = (input) => string.Join("", input.SkipWhile(f => !char.IsDigit(f)));
                var ex1 = getIntPart(s1);
    
                var ex2 = getIntPart(s2);

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 30, 2016 1:34 AM
  • You could refer this.

    Lot of examples for this requirements.

    http://stackoverflow.com/questions/4734116/find-and-extract-a-number-from-a-string

    Hope this helps.

    Friday, December 30, 2016 2:05 AM