How do you remove all the alphabetic characters from a string?

后端 未结 6 1580
时光说笑
时光说笑 2020-12-24 10:18

I have a string containg alphabetic characters, for example:

  1. 254.69 meters
  2. 26.56 cm
  3. 23.36 inches
6条回答
  •  心在旅途
    2020-12-24 10:56

    Hi another solution is:

    // add directive at the top 
    using System.Text.RegularExpressions;
    string s = "24,99";
    string numberOnly = Regex.Replace(s, "[^0-9,-]+", "")
    

    This solution will not remove the dot e.g. question from user1804084:

    this remove the dot for me e.g. 24.99 somechracter -> 2499

    However it can still be convertet to a double where adding and subtraction works as normal.

提交回复
热议问题