How to remove digits / digits from strings in List <string>?

I have a list of lines:

List<string> _words = ExtractWords(strippedHtml); 

_words contains 1799 indexes; each index has a row.

Some lines contain only numbers, for example:

" 2" or "2013"

I want to delete these lines, and in the end the list will contain only lines with letters, not numbers.

A string like "001hello" is okay, but "001" not okay and needs to be deleted.

+4
source share
4 answers

You can use LINQ to do this:

 _words = _words.Where(w => w.Any(c => !Char.IsDigit(c))).ToList(); 

This will filter out strings consisting solely of numbers, along with empty strings.

+5
source
 _words = _words.Where(w => !w.All(char.IsDigit)) .ToList(); 
+4
source

To remove words that are made only of numbers and spaces:

 var good = new List<string>(); var _regex = new Regex(@"^[\d\s]*$"); foreach (var s in _words) { if (!_regex.Match(s).Success) good.Add(s); } 

If you want to use LINQ, something like this:

 _words = _words.Where(w => w.Any(c => !char.IsDigit(c) && !char.IsWhiteSpace(c))) .ToList(); 
+2
source

You can use traditional foreach and Integer.TryParse to detect numbers. It will be faster than Regex or LINQ.

 var stringsWithoutNumbers = new List<string>(); foreach (var str in _words) { int n; bool isNumeric = int.TryParse(str, out n); if (!isNumeric) { stringsWithoutNumbers.Add(str); } } 
+1
source

Source: https://habr.com/ru/post/1488550/


All Articles