首页 > Windows开发 > 详细

C#中的字符串处理——找出最长数字子串

时间:2015-11-11 14:55:06      阅读:322      评论:0      收藏:0      [点我收藏+]

百度测试部2015年10月份的面试题之——字符串处理,找出最长的子串。

代码如下:

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#写就是方便不少,但是感觉百度好像不喜欢微软技术,是吗?

如果您有其他版本的解法,欢迎添加进来一起学习。

有时间我会陆续解决其他的面试题,欢迎关注。

C#中的字符串处理——找出最长数字子串

原文:http://www.cnblogs.com/LanTianYou/p/4955819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!