C#练习题答案: 修复我的电话号码!【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

修复我的电话号码!【难度:1级】:

答案1:

using System;
using System.Text.RegularExpressions;


public class Kata {
    public static String IsItANum(string str) {
        string number = Regex.Replace(str, @"\D", "", RegexOptions.None, TimeSpan.FromSeconds(1));
        if (number.Length == 11 && number[0] == '0')
            return number;
        else
            return "Not a phone number";
    }
}

答案2:

using System;
using System.Text.RegularExpressions;

public class Kata
{
  public static string IsItANum(string str) {
    string result = new Regex(@"[^\d]").Replace(str, "");
    return (result.Length == 11 && result[0] == '0') ? result : "Not a phone number";
  }
}

答案3:

using System.Text.RegularExpressions;

public class Kata
{
    public static string IsItANum(string str)
    {
        string d = Regex.Replace(str, @"\D", "");
        var r = Regex.Match(d, @"^0[0-9]{10}$");
        return r.Success ? r.Value : "Not a phone number";
    }
}

答案4:

using System;
public class Kata
{
  public static String IsItANum(string str) {
            string num = "";
            for (int i = 0; i < str.Length; i++)
            {
                if ((int)str[i] > 47 &amp;&amp; (int)str[i] < 58)
                {
                    num = num + str[i].ToString();
                }
            }
            return num.Length == 11 &amp;&amp; num[0] == '0'?num: "Not a phone number";
  }
}

答案5:

using System;
using System.Linq;

public class Kata
{
    public static String IsItANum(string str)
    {
        var number = "";

        foreach (var val in str)
        {
            if (char.IsDigit(val))
            {
                number += val;
            }
        }

        return number.Length == 11 &amp;&amp; number.First() == '0' ? number : "Not a phone number";
    }
}

答案6:

using System;
using System.Text.RegularExpressions;

public class Kata
{
  public static String IsItANum(string str)
        {
            string STR = Regex.Replace(str, "[^0123456789]", "");

            if (STR.Length == 11 &amp;&amp; STR[0] == '0')
                return STR;

            return "Not a phone number";
        }
}

答案7:

using System;
using System.Text;

public class Kata {
    public static String IsItANum( string str ) {
        var number = new StringBuilder( );
        foreach ( var c in str ) {
            if ( char.IsDigit( c ) ) {
                number.Append( c );
            }
        }
        var result = number.ToString( );
        if ( result.StartsWith( "0" ) &amp;&amp; result.Length == 11 ) {
            return result;
        }
        return "Not a phone number";
    }
}

答案8:

using System.Text.RegularExpressions;
using System;

public class Kata
{
  public static String IsItANum(string str) {
    Regex digits = new Regex(@"[^\d]");
    
    string digitsOnly = digits.Replace(str, "");
    
    return digitsOnly.Length == 11 &amp;&amp; digitsOnly[0] == '0' ? digitsOnly : "Not a phone number";
  }
}

答案9:

using System.Text.RegularExpressions;
public class Kata
{
  public static string IsItANum(string str) 
  {
   string s = Regex.Replace(str,@"[^\d]+","");
   if (Regex.IsMatch(s,@"^0(\d){10}$"))
     return s;
     else return "Not a phone number";
  }
}

答案10:

using System.Text.RegularExpressions;

public class Kata
{
  public static string IsItANum(string str)
  {
    string s = Regex.Replace(str, @"\D", "");
    return Regex.IsMatch(s, @"^0\d{10}$") ? s : "Not a phone number";
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值