zoukankan      html  css  js  c++  java
  • C#中的字符串处理——找出最长数字子串

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

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

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

  • 相关阅读:
    isalnum()方法
    index()方法
    find()方法
    expandtabs()方法
    endswith()方法
    encode()方法
    bytes.decode()方法
    count()方法
    center()方法
    capitalize()方法
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/4955819.html
Copyright © 2011-2022 走看看