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); } } }
Regex.Replace(str, @"(?<=\.\d\d)\d+", "")- PetSerAlRegex.Replacesaved? - Andrew NOP