2009-10-29 12 views

Respuesta

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

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

Puede crear una extensión de cadena para ocultar el uso de Any

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

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

_cualquier_ .. ah !!! increíble :) amo a Linq. gracias amigo :) –

0

No hay nada integrado en el marco .NET, pero aquí es un método de extensión que va a hacer el truco :

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

Cheers andrew. Sí, esto es (más o menos) lo que ya tengo. Quería ver cómo hacer esto como Linq (para que yo pueda aprenderlo). –

+0

complemento! jajajaja :-) –

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; 
} 
} 
Cuestiones relacionadas