How to strip all non-numeric characters from string in C#

2 Answers

0 votes
using System;
using System.Linq;

class Program
{
    private static string removeNonNumeric(string s) {
        return new string(s.Where(c => char.IsDigit(c)).ToArray());
    }
    static void Main() {
        string s = "2a1-0/R@a9f#4K$$cC3K^htPam8vlQWhJ";

        s = removeNonNumeric(s);
        
        Console.Write(s);
    }
}



/*
run:

2109438

*/

 



answered Jun 15, 2020 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main() {
        string s = "2a1-0/R@a9f#4K$$cC3K^htPam8vlQWhJ";

        s = Regex.Replace(s, "[^.0-9]", "");
        
        Console.Write(s);
    }
}



/*
run:

2109438

*/

 



answered Jun 15, 2020 by avibootz

Related questions

2 answers 144 views
1 answer 136 views
2 answers 208 views
2 answers 222 views
2 answers 129 views
...