2009-10-08 29 views

Respuesta

145
"abc3def".Any(c => char.IsDigit(c)); 

actualización: @Cipher como ha señalado, en realidad puede ser aún más corto:

"abc3def".Any(char.IsDigit); 
+0

No pude encontrar todos los métodos() y Any() aunque estoy usando 4.5 marco de referencia. ¿Sabes por qué? – cihata87

+4

@ cihata87 asegúrese de haber agregado 'using System.Linq;' en la parte superior del archivo de código. –

12

Prueba este

public static bool HasNumber(this string input) { 
    return input.Where(x => Char.IsDigit(x)).Any(); 
} 

Uso

string x = GetTheString(); 
if (x.HasNumber()) { 
    ... 
} 
+3

O simplemente 'input.Any (x => Char.IsDigit (x));' –

+0

@Mehrdad, sí constantemente me olvido de esa sobrecarga – JaredPar

8

o posible uso de expresiones regulares:

string input = "123 find if this has a number"; 
bool containsNum = Regex.IsMatch(input, @"\d"); 
if (containsNum) 
{ 
//Do Something 
} 
-1

¿Qué tal esto:

bool test = System.Text.RegularExpressions.Regex.IsMatch(test, @"\d"); 
-1
string number = fn_txt.Text; //textbox 
     Regex regex2 = new Regex(@"\d"); //check number 
     Match match2 = regex2.Match(number); 
     if (match2.Success) // if found number 
     { **// do what you want here** 
      fn_warm.Visible = true; // visible warm lable 
      fn_warm.Text = "write your text here "; /
     } 
+0

No creo que esto realmente responda la pregunta, ya que la pregunta estaba interesada en consultas breves, y ya hay muchas que son mucho más concisas que esta. – JHobern

Cuestiones relacionadas