Suppose given the string "ab + 0.1973-1.1"

It is processed character by foreach. It is necessary to remove from each group of consecutive digits, in which there are more than two digits and preceded by a dot, all digits, starting with the third one.

What is it done with? Found a way to remove characters to a certain sign, but there is no definite sign, because a string may be given, for example, "ab + 0.1973 + 1.1" and will no longer work.

I know what else you can through Remove, but I do not know how.

Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _270г { class Program { static void Main(string[] args) { int flag,k,ch; flag = 0; k = 0; ch = 0; string str = "ab+0.1973-1.1"; foreach (char c in str) { ch++; if ('.' == c) { flag = 1; } if ((flag == 1) && (char.IsDigit(c))) { k++; if (k == 3) { while (char.IsDigit(c)) { str = str.Remove(ch-1,1); } } } } Console.WriteLine(str); } } } 
  • 2
    Regex.Replace(str, @"(?<=\.\d\d)\d+", "") - PetSerAl
  • Added, nothing is displayed. - Kirill Himov
  • And the result of the call Regex.Replace saved? - Andrew NOP
  • In the first two if-ah you expect the character to be both a point and a number. And the foreach here is not really to the point, I would rather use while until the end of the line - yolosora
  • If your flag is always 1 or 0, then why not use the bool type? - yolosora

0