简体   繁体   中英

using EndsWith() inside List.Any() is not working as expected

I have the following code inside my c# console application, where i want to check if a string cleanedphone end with any entry inside a list of strings:-

static List<string> Mylist;
var cleanedphone = new string(phone.Trim().Replace(" ", "").Where(c => char.IsDigit(c)).ToArray());//remove non numeric chars + white spaces
si.status = Mylist.Any(a => cleanedphone.EndsWith(a)) ? "Yes" : "No";

but in my case the.Any() will return Yes even if the cleanedphone does not end with any entry inside the Mylist ... so can anyone advice? Thanks

The issue with your code is that your List contains an Empty String. An easier way forward would be to remove the empty string.

var status = Mylist.Where(a=>a.Length>0).Any(a => cleanedphone.EndsWith(a)) ? "Yes" : "No";

Or combining the conditions

var status = Mylist.Any(a => a.Length>0 && cleanedphone.EndsWith(a)) ? "Yes" : "No";

As a footnote, it would be better if you could use Regex for cleaning up the phone number for non-numeric characters. For example,

var cleanedphone = Regex.Replace(phone, @"\D", string.Empty);

The problem is cause by the empty string in the list. String.EndsWith returns true for every string if the input is an empty string, eg:

"abc".EndsWith("");

Returns true.

You'll have to clean up the list of suffixes, eg with:

myList=myList.Where(x=>!String.IsNullOrWhitespace(x)).ToList();

You can speed up and simplify your code if you use a regular expression stored in a static field to remove non-numeric characters, eg:

static Regex _cleanup;
static List<string> _suffixes;

static void Initialize(string sourcePath)
{
    _cleanup  = new Regex("\\D");
    _suffixes = File.ReadLines(sourcePath)
                    .Where(x=>!String.IsNullOrWhitespace(x))
                    .ToList();
}

public string CheckPhone(string phone)
{
    var cleanedphone = _cleanup.Replace(phone,"");
    var result = _suffixes.Any(a => cleanedphone.EndsWith(a)) ? "Yes" : "No";
    return result;
}


`File.ReadLines` returns an `IEnumerable<string>` instead of `string[]` which means you don't have to load the entire file before cleaning up the entries

This works for me:

class Program
{
    static void Main(string[] args)
    {
            string input = @"58181
09905352823
09912713815
09913842311
09915345235
09921174632
0992578810 
0992711803 
0995640639 
09965262212
09971159292
0997626372 
09988772816
09999100244

01132171481
01132672788
01133804928
01142692085
01142880116
01142962865
0114457953
01787227855
01788438359
01788536032
01788561185
01788574002
01788878949
01789721106
07594135168
07594189551
07594538828
07594791499
01912372953
01912375775
01912376383
01912415145
01912434658
01912502728
07989244376
07989384524
07989491312
07989549434";

        List<string> numbers = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

        string cleanedPhone = "992043411000";

        string status = numbers.Any(x => cleanedPhone.EndsWith(x)) ? "Yes" : "No";

        Console.WriteLine(status);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM