2009-10-29 4 views

Antwort

12
var collection = new string[] { "ny", "er", "ty" }; 

var doesEnd = collection.Any("Johnny".EndsWith); 
var doesNotEnd = collection.Any("Fred".EndsWith); 

können Sie eine String-Erweiterung erstellen die Verwendung von Any

public static bool EndsWith(this string value, params string[] values) 
{ 
    return values.Any(value.EndsWith); 
} 

var isValid = "Johnny".EndsWith("ny", "er", "ty"); 
+0

_jeder_ .. ah !!! genial :) Ich liebe Linq. Danke Kumpel :) –

0

Es zu verstecken ist nichts in dem .NET-Framework aufgebaut, aber hier ist eine Erweiterung Methode, die den Trick tun :

public static Boolean EndsWith(this String source, IEnumerable<String> suffixes) 
{ 
    if (String.IsNullOrEmpty(source)) return false; 
    if (suffixes == null) return false; 

    foreach (String suffix in suffixes) 
     if (source.EndsWith(suffix)) 
      return true; 

    return false; 
} 
+0

Prost andrew. Ja, das ist (mehr oder weniger) was ich schon habe. Ich wollte sehen, wie man das als Linq macht (damit ich es lernen kann). –

+0

schnappen! hahahah :-) –

0
public static class Ex{ 
public static bool EndsWith(this string item, IEnumerable<string> list){ 
    foreach(string s in list) { 
    if(item.EndsWith(s) return true; 
    } 
    return false; 
} 
} 
Verwandte Themen