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
4 answers
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
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