编程语言
首页 > 编程语言> > C#练习题答案: 查找组合【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

C#练习题答案: 查找组合【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

作者:互联网

查找组合【难度:1级】:

答案1:

using System.Text.RegularExpressions;

public class Kata
{
  public static int CountCombinations(string text, string key)
  {  
    return Regex.Matches(text, key, RegexOptions.IgnoreCase).Count;
  }
}​

答案2:

using System;
using System.Text.RegularExpressions;

public class Kata
{
    public static int CountCombinations(string text, string key)
    {
        return Regex.Matches(text.ToLower(), key.ToLower()).Count;
    }
}​

答案3:

using System.Text.RegularExpressions;

public class Kata {
    public static int CountCombinations( string text, string key ) {
        if ( string.IsNullOrEmpty( key ) ) {
            return 0;
        }
        var pattern = key.Replace( "\\\\d", "\\d" ).Replace( "\\.", "." );
        return Regex.Matches( text, pattern, RegexOptions.IgnoreCase ).Count;
    }
}​

答案4:

using System.Text.RegularExpressions;

public class Kata
{
  public static int CountCombinations(string text, string key) => new Regex(key, RegexOptions.IgnoreCase).Matches(text).Count;
}​

答案5:

using System;
using System.Linq;

public class Kata
{
    public static int CountCombinations(string text, string key)
    {
        Console.WriteLine(key + ": " + text);
    
        if (key == "\\d" || key == ".")
            return text.Length;
        if (key == "\\.")
            return GetNumber(text);  
        if (key == "\\s")
            return text.Count(x => x == ' ');

        return text.ToLower().Replace(key.ToLower(), "Ö").Count(x => x == 'Ö');
    }

    private static int GetNumber(string text)
    {
        switch (text.ToLower())
        {
            case "one": return 1;
            case "two": return 2;
            case "three": return 3;
            case "four": return 4;
            case "five": return 5;
            case "six": return 6;
            case "seven": return 7;
            case "eight": return 8;
            case "nine": return 9;
        }

        return 0;
    }
}​

答案6:

using System.Text.RegularExpressions;
using System.Linq;
using System;
public class Kata
{
  public static int CountCombinations(string text, string key)
  {
    Regex regex = new Regex(key.ToLower());
    return regex.Matches(text.ToLower()).Cast<Object>().Count();
  }
}​

答案7:

using System;
using System.Linq;
public class Kata
{
  public static int CountCombinations(string text, string key)
  {
    if(key == "\\s") key = " ";
    if(key == "\\d" || key == ".") return text.Length;
    return (text.ToLower().Replace(key, "").Length - text.Length) / key.Length * -1;
  }
}​

答案8:

using System;
using System.Text.RegularExpressions;
public class Kata
{
  public static int CountCombinations(string text, string key)
  {
    int count=0;
  foreach (Match m in Regex.Matches(text.ToLower(), key.ToLower()))
  count++;
    
    return count;
  }
}​

答案9:

using System.Text.RegularExpressions;

public class Kata
{
  public static int CountCombinations(string text, string key) => Regex.Split(text.ToLower(), key).Length - 1;
}​

答案10:

using System.Text.RegularExpressions;
public class Kata
{
  public static int CountCombinations(string text, string key)=>new Regex(key.ToLower()).Matches(text.ToLower()).Count;
}​




标签:练习题,return,景越,C#,text,System,key,public,string
来源: https://blog.csdn.net/weixin_45444821/article/details/101095993