我想知道如果一個字符串包含任何數字字符,最簡單和最短的LINQ查詢將返回true。檢查一個字符串是否至少有一個使用LINQ的編號
56
A
回答
145
12
試試這個
public static bool HasNumber(this string input) {
return input.Where(x => Char.IsDigit(x)).Any();
}
使用
string x = GetTheString();
if (x.HasNumber()) {
...
}
+3
或者只是'input.Any(x => Char.IsDigit(x));' – 2009-10-08 21:33:02
+0
@Mehrdad,是的,我經常忘記那個過載 – JaredPar 2009-10-08 21:35:17
8
或可能使用正則表達式:
string input = "123 find if this has a number";
bool containsNum = Regex.IsMatch(input, @"\d");
if (containsNum)
{
//Do Something
}
-1
如何:
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
我不認爲這真的回答了這個問題,因爲這個問題對短期查詢感興趣,並且已經有很多比這更簡潔的問題了。 – JHobern 2015-10-30 22:00:51
相關問題
- 1. 如何使用Ruby檢查字符串中是否至少有一個數字?
- 2. 如何檢查字符串是否至少有6個字符,一個大寫字母和一個數字? - Javascript
- 3. 如何檢測一個字符串是否至少包含一個數字?
- 4. 如何檢查生成的字符串是否至少包含一個字母
- 5. 如何檢查至少有一個元素形成一個字符數組是否在字符串中? (Java)
- 6. 檢查輸入字符串中是否至少出現一個非數字值
- 7. 檢查一組電臺是否至少有一個選中
- 8. LINQ SkipWhile - 至少有一個
- 9. 如何檢查一個字符串是否至少包含一箇中文字符?
- 10. Inno設置如何檢查一個字符串是否包含至少一個特殊字符
- 11. 如何檢查一個字符串中是否至少有6個字符在JavaScript中?
- 12. 檢查字典中是否至少包含一個字典
- 13. 檢查是否至少有一個複選框使用HTML5進行檢查
- 14. 檢查一個集合是否至少包含一個文檔
- 15. 有效檢查一個字符串是否包含另一個字符串
- 16. 檢查是否至少有一個元素是不同
- 17. 檢查是否至少有一個孩子有XSLT 1.0
- 18. 檢查字符串多少次是一個字符串
- 19. 如何檢查一個字符是否是一個撇號?
- 20. vb.net如何檢查一個字符串是否有某個字
- 21. 檢查兩個輸入字段中的至少一個是否有數據
- 22. 檢查的第二個字符串中的所有字符是否與存在於第一個字符串中的字符匹配至少一次
- 23. 查找包含至少一個組的所有子字符串
- 24. 檢查一個字符串是否有特殊字符
- 25. 檢查一個字符串是否是一個有效的數字:ArrayIndexOutOfBoundsException異常
- 26. 如何檢查一個對象是否是一個字符串?
- 27. 如何檢查至少包含一個字母和一個數字的字符串字符串
- 28. 檢查是否至少有一個複選框被選中
- 29. javascript - 如何檢查數組是否至少有一個負值?
- 30. jquery - 檢查是否至少有一個複選框被選中
@saul最新的成交與賞金?明顯的答案是可信的。 – 2014-11-12 05:12:34