How to extract all digits from string in C#

2 Answers

0 votes
using System;

public class Program {
    public static void Main() {
        String s = "abc 9836 xy%^(s 21 * 9 ppp 100";

        String digits = "";
        
        foreach (char ch in s) {
            if (!Char.IsDigit(ch)) {
                continue;
            }
            digits += ch;
        }
        
        Console.WriteLine(digits);
    }
}



/*
run:

9836219100

*/

 



answered Dec 16, 2020 by avibootz
0 votes
using System;
using System.Linq;

public class Program {
    public static void Main() {
        String s = "abc 9836 xy%^(s 21 * 9 ppp 100";

        String digits = new String(s.Where(Char.IsDigit).ToArray());

        Console.WriteLine(digits);
    }
}



/*
run:

9836219100

*/

 



answered Dec 16, 2020 by avibootz

Related questions

1 answer 163 views
1 answer 137 views
1 answer 174 views
1 answer 134 views
1 answer 133 views
133 views asked Dec 16, 2020 by avibootz
1 answer 154 views
...