Find consecutive numbers in a string in C#

Asked

Viewed 539 times

0

I wonder if there is a simple way to find consecutive numbers (which are equal) in a string, so that it is possible to replace that string with a single value.

For example:

string input = "Abc111de2234";
string output = "Abc1de234";
  • @Diegorafaelsouza Besides this scan, is there a more efficient way to do this? Why in this case would it take two 'for', no?

  • 1

    Why do you think you’d need two for?

  • @Maniero I had thought to fix the current character within the first is, and if this character was a number, start another from the next position to check if the condition proposed by Diego would be true. You could do it with just one for?

  • 2

    I can’t imagine why this would be necessary. Either do it with all characters or just digits?

  • @Maniero the substitution I want to do is only with the numbers present in the string. The point is that this string can have letters and numbers together. So the proposal would be to identify only numbers, which are equal and adjacent to replace.

2 answers

9

I would do so:

using static System.Console;

public class Program {
    public static void Main() {
        var texto = "Abcc111de12234";
        var lista = new char[texto.Length];
        for (int i = 0, j = 0; i < texto.Length; i++) if (!char.IsDigit(texto[i]) || (i == 0 || texto[i] != texto[i - 1])) lista[j++] = texto[i];
        WriteLine(new string(lista));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

The solution of the other answer generates several allocations and copies of string, making the application slow and consuming a lot of memory, apart from causing breaks. It has other inefficiencies.

  • 2

    Thanks for the answer! It’s always nice to see a problem being solved in different ways, mainly to observe efficiency :)

  • That answer should be accepted

-1


It is possible to scan the characters with a single loop, (Example in dotnetfiddle):

string input = "Abc111de2234";
string output = string.Empty; // Desejado: "Abc1de234";
char[] numbers = new []{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

if(!string.IsNullOrWhiteSpace(input))
    for(int i = 0; i < input.Length; i++)
        if (i > 0)
        {
            if (numbers.Contains(input[i]))
            {
                if(input[i] != input[i-1])
                    output += input[i];
            }
            else
                output += input[i];
        }
        else
            output += input[i];

Console.WriteLine(output);

Other forms of doing include the use of regular expressions, for example, but I believe this would be less performative.

  • 1

    Thanks, that’s what I was trying to do (only before with two is, being something much less efficient and that was presenting problems in the output). I hadn’t thought to use an array with the numbers to help in the comparisons, in fact this helps a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.