Remove unwanted characters from a List<string> without going through the list

Asked

Viewed 236 times

6

Be the following excerpt a Windows Forms application.

List<string> lista_strings = new List<string>();

for (int i = 0; i < 2000; i++)
{
    lista_strings.Add("TST1234"); //preenchendo a lista
}

for (int i = 0; i < lista_strings.Count; i++)
{
    lista_strings[i] = lista_strings[i].Substring(3); //retirando os caracteres
                                                      //iniciais de cada posição
}

I made a bond for so that, from position to position, I can remove the initial characters of each string("TST") inside the list of strings lista_strings. There is a form of remove the initial characters from the whole list without having to change their values one at a time?

  • It doesn’t seem very clear what you want. The description is a bit confusing. Is there something wrong with your code? Is there anything else you want to do? Has some desired improvement?

  • In my application I always have new elements in the list of strings so I think it doesn’t pay to go through the whole list. It always has three initial characters in all strings and concatenates the number I want. I want to remove them and then convert to int. This is just one example.

  • That I understand, that’s very clear, but what do you want us to answer?

  • How to remove characters in a simpler way, without scrolling through the entire list, since I’m sure the characters will be present in all strings.

  • 1

    If you want to delete across the list how do you think it is possible to do this without going through the entire list? Impossible.

2 answers

7

There is a way to remove initial characters from every list without having to change their values one at a time?

No, it does not. The definition of the problem itself already indicates this. If you want to go through all the elements of the list to change your content the only way is to go through all the elements.

If the question was without using a loop I could say it depends. It’s virtually impossible to do without a loop, one way or another. The only way is to manually make each element, which is crazy.

Then you must be saying it, but the other answer showed that you can do it. But in fact you can’t. Can you hide the loop, as you can do in any function, as you can do whenever you create a abstraction. The point is that . NET already has an abstraction ready called LINQ.

What LINQ does is scroll through enumerable objects, as is the case with the list. How does it scroll through a list? Through a loop. There’s no magic, only she’s inside a method.

So is it always better to use LINQ? No. It depends on what you want to do. If you want to manipulate the data more complex, LINQ might not be a good one. If you need performance LINQ does not suit you well, it has a considerably higher cost when this is important. Test with both.

But this is the only cost? No, it creates a new list, makes new allocation and is inefficient and can cause unwanted breaks in the application more often.

Of course, all this depends on a number of factors, but if you use indiscriminately without knowing how each abstraction works will get involved in problems, so you need to understand everything and make the most appropriate decisions. Can’t be the choice of the cutest.

Is it much shorter and obvious? No, just a little, see the difference:

for (int i = 0; i < lista.Count; i++) if (s.Length > 3) lista[i] = lista[i].Substring(3);
lista = lista.Select(s => s.Length > 3 ? s.Substring(3) : s).ToList();

I put in the Github for future reference.

6


We can use the Linq to effect the modification of string more simply, because from what I understand you want to remove the loop for.

Below is the example with Linq:

using System.Linq;

lista_strings = lista_strings.Select(s => s.Length > 3 ? s.Substring(3) : s).ToList();

Here in this iteration we are taking each string from our list, which is being represented by s in the Select(), after that we check if the size of this string is bigger than 3 to avoid throwing Exception if you do not have the required number of characters, if you have more than 3 characters we will perform the Substring(3) (as you already did), otherwise we take the content the way it is currently.

Browser other questions tagged

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