C#练习题答案: 正则表达式基础 - 它是一个八位数签约?【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客主要介绍了C#中如何使用正则表达式判断一个字符串是否为八位数,提供了10种不同的解决方案,适合初学者巩固正则表达式和C#编程基础。
摘要由CSDN通过智能技术生成

正则表达式基础 - 它是一个八位数签约?【难度:1级】:

答案1:

using System.Text.RegularExpressions;

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
    {
        return Regex.IsMatch(s, @"\A(12[0-7]|1[01][0-9]|[1-9]?[0-9]|-(12[0-8]|1[01][0-9]|[1-9][0-9]|[1-9]))\z");
    }
}

答案2:

using System;
using System.Text.RegularExpressions;

public static class Kata
{
  public static Boolean SignedEightBitNumber(this String Input)
  {
    return Regex.IsMatch(Input, "^(0|(\\-128)|(\\-?((12[0-7])|(1[0-1][0-9])|([1-9][0-9]{0,1}))))\\z");
  }
}

答案3:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
    {
        return Enumerable.Range(sbyte.MinValue, (int)Math.Pow(2.0, sizeof(sbyte) * 8)).Any(j => j.ToString() == s);
    }
}

答案4:

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
    {      
        for (int i = -128; i<128; i++)                  
           if (s == "" +  i) return true;        
           
        return false;
    }    
}

答案5:

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
    {
        sbyte v = 0;
        return sbyte.TryParse(s, out v) ? v.ToString() == s : false;
    }
}

答案6:

using System;
using System.Text.RegularExpressions;

public static class Kata
{
  public static bool SignedEightBitNumber(this string s)
  {
    try
    {
      if (!Regex.IsMatch(s, @"\A(0|-?[1-9]\d*)\z"))
        return false;
      int n = int.Parse(s);
      return n > -129 &amp;&amp; n < 128 ? true : false;
    }
    catch (FormatException) { return false; }
  }
}

答案7:

using System.Text.RegularExpressions;

public static class Kata {
    public static bool SignedEightBitNumber( this string s ) {
        return Regex.IsMatch( s,
            @"\A(([0-9])|([1-9][0-9])|([1][0-1][0-9])|(12[0-7])|(-[1-9][0-9]?)|(-12[0-8])|(-[1][0-1][0-9]))\z" );
    }
}

答案8:

using System;
using System.Text;
using System.Text.RegularExpressions;

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
        => Regex.IsMatch(s, @"\A(-12[0-8]|-1[0-1][0-9]|-[1-9][0-9]?|12[0-7]|1[0-1][0-9]|[1-9][0-9]?|0)\z");
}

答案9:

using System.Text.RegularExpressions;

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
    {
        int num;
        return !s.Contains("\n") &amp;&amp; RegexValidate(s) &amp;&amp; int.TryParse(s, out num) &amp;&amp; num >= -128 &amp;&amp; num <= 127;
    }
    
    static bool RegexValidate(string s)
    {
        return !Regex.IsMatch(s, @"^0\d|\-0| |\+");      
    }
}

答案10:

using System;
using System.Text;
using System.Text.RegularExpressions;

public static class Kata
{
    public static bool SignedEightBitNumber(this string s)
        {
        if(s.Contains(" ") || s.Contains("\n") || s.Contains("+")) return false;
        if(s == "0") return true;
        int k = 129;
        if(s.Length > 0 &amp;&amp; s[0] == '0') return false;
        if(s.Length > 1 &amp;&amp; s[0] == '-' &amp;&amp; s[1] == '0') return false;
        if (int.TryParse(s, out k)) return k!= 0 &amp;&amp; k > -129 &amp;&amp; k < 128;
        return false;
        }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值