2009-10-29 8 views

Odpowiedz

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

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

Można utworzyć rozszerzenie String ukryć wykorzystanie Any

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

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

sprzedawane a jakimkolwiek .. ah !!! niesamowite :) kocham Linq. dzięki kolego :) –

0

Nic wbudowane w ramach .NET, ale tutaj jest metoda rozszerzenie, które rade :

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

Pozdrawiam andrew. tak, to jest (mniej więcej) to, co już mam. Chciałem zobaczyć, jak to zrobić jako Linq (aby móc się tego nauczyć). –

+0

snap! 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; 
} 
} 
Powiązane problemy