There is a type such number 224511, it is hammered into the TexBox. It must be done so that when checking the text and getting two identical numbers, he put the ## symbol. Something like this.

  • four
    Can you describe the task more precisely? And at least try to write better. I also ask you to pay attention that no one will do the work for you, but you can always tell where and what is the error in your own work. - Spawn
  • You need to create a comparison loop. That is, if in the visible numbers there are 2 identical figures, then you should put the # symbol in front of them. What would happen is this: an example 225788 were ## 57 ## received. I do not ask you to decide, I ask you to direct me in the right direction. - SpiritAT

2 answers 2

As I understand it, you need to replace pairs of consecutive identical characters.

string str = "2233445123"; char[] strArr = str.ToCharArray(); for (int i = 0; i < strArr.Length - 2; i+=2) { if (strArr[i].Equals(str[i + 1])) { strArr[i] = '#'; strArr[i + 1] = '#'; } } str = string.Join(string.Empty, strArr); 
  • Yes, this joint venture is great - SpiritAT
  • @SpiritAT, close the questions if you are satisfied with the answer. - Max Zhukov
  • How to do it? - SpiritAT
  • 2
    btw, counterexamples to the solution: 99988 - it should be ## 9 ##, 9 #### or ##### 55555 - it should be #### 5, 5 #### or ##### (here are a couple of options) although this is the author of the question :)) - KoVadim
  • 99988 - this should be ## 9 ## - SpiritAT

You can make a replacement using a simple regular schedule:

 var rex = new Regex(@"(.)\1"); var replaced = rex.Replace("99988", "##"); Console.WriteLine(replaced); // -> ##9## 

Demonstration of work: https://dotnetfiddle.net/xML8n2