Finding all numbers in a string

前端 未结 2 1675
醉话见心
醉话见心 2020-12-10 20:52

Part of my app has an area where users enter text into a textBox control. They will be entering both text AND numbers into the textBox. When the user pushes a button, the te

2条回答
  •  我在风中等你
    2020-12-10 21:11

    You can use a regular expression that matches the numbers, and use the Regex.Replace method. I'm not sure what you include in the term "numbers", but this will replace all non-negative integers, like for example 42 and 123456:

    str = Regex.Replace(
      str,
      @"\d+",
      m => (Double.Parse(m.Groups[0].Value) * 1.14).ToString()
    );
    

    If you need some other definition of "numbers", for example scientific notation, you need a more elaboarete regular expression, but the principle is the same.

提交回复
热议问题