Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove numbers/digits from strings in a List<string>?

Tags:

c#

linq

I have a List of strings:

List<string> _words = ExtractWords(strippedHtml);

_words contains 1799 indexes; in each index there is a string.

Some of the strings contain only numbers, for example:

" 2" or "2013"

I want to remove these strings and so in the end the List will contain only strings with letters and not digits.

A string like "001hello" is OK but "001" is not OK and should be removed.

like image 237
Haim Kashi Avatar asked Jun 27 '13 17:06

Haim Kashi


4 Answers

You can use LINQ for that:

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

This would filter out strings that consist entirely of digits, along with empty strings.

like image 139
Sergey Kalinichenko Avatar answered Nov 03 '22 01:11

Sergey Kalinichenko


_words = _words.Where(w => !w.All(char.IsDigit))
               .ToList();
like image 41
cuongle Avatar answered Nov 02 '22 23:11

cuongle


For removing words that are only made of digits and whitespace:

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 should do:

_words = _words.Where(w => w.Any(c => !char.IsDigit(c) && !char.IsWhiteSpace(c)))
               .ToList();
like image 22
ctn Avatar answered Nov 03 '22 01:11

ctn


You can use a traditional foreach and Integer.TryParse to detect numbers. This 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);
    }
}
like image 39
Fabian Bigler Avatar answered Nov 03 '22 00:11

Fabian Bigler