C#中的字符串處理——找出最長數字子串

百度測試部2015年10月份的面試題之——字符串處理,找出最長的子串。html

代碼以下:面試

private static string SelectNumberFromString(string input)
{
    string result = "";
    foreach (Match match in Regex.Matches(input, @"\d+"))//不要在匹配字符串的開頭和結尾加上"^"和"$"。
    {
        result = match.Value.Length > result.Length ? match.Value : result;
    }
    return result;
}

測試代碼以下:學習

static void Main(string[] args)
{
    string input = Console.ReadLine();
    string result = SelectNumberFromString(input);
    Console.WriteLine(result);
    Console.ReadKey();
}

測試數據與結果:測試

用C#寫就是方便很多,可是感受百度好像不喜歡微軟技術,是嗎?spa

若是您有其餘版本的解法,歡迎添加進來一塊兒學習。code

有時間我會陸續解決其餘的面試題,歡迎關注。htm

相關文章
相關標籤/搜索