How to remove the letters (all non-numeric) from a string in C#

2 Answers

0 votes
using System;
using System.Linq;

class Program
{
    public static string RemoveAllLetters(string s) {
        return new string(s.Where(ch => char.IsDigit(ch)).ToArray());
    }

    static void Main() {
        string str = "1java23c#908c++674c";
        
        str = RemoveAllLetters(str);

        Console.Write(str);
    }
}




/*
run:

123908674

*/

 



answered Jan 28, 2017 by avibootz
edited Aug 26, 2023 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;
 
class Program
{
    static void Main() {
        string str = "1java23c#908c++674c";
         
        str = Regex.Replace(str, "[^0-9.]", "");
 
        Console.Write(str);
    }
}
 
 
 
 
/*
run:
 
123908674
 
*/

 



answered Aug 26, 2023 by avibootz

Related questions

1 answer 180 views
2 answers 181 views
2 answers 277 views
2 answers 266 views
1 answer 218 views
...