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 157 views
1 answer 131 views
1 answer 170 views
1 answer 129 views
1 answer 125 views
125 views asked Dec 16, 2020 by avibootz
1 answer 143 views
...