检查字符串是否所有字符是十六进制值 [英] Check a string to see if all characters are hexadecimal values

查看:60
本文介绍了检查字符串是否所有字符是十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C#2.0的最有效的方法来检查每个字符的字符串,如果他们都是有效的十六进制字符,否则为假?

返回true

示例

 无效测试()
{
    OnlyHexInString(123ABC); //返回true
    OnlyHexInString(123def); //返回true
    OnlyHexInString(123克); //返回false
}布尔OnlyHexInString(字符串文本)
{
    //最有效的算法来检查每个数字在C#2.0到这里
}


解决方案

 公共BOOL OnlyHexInString(字符串测试)
{
    //对于C风格的16进制(0xFF的),你可以用@\\ A \\ B(0 [XX])?[0-9A-FA-F] + \\ B \\ Z
    返回System.Text.RegularEx pressions.Regex.IsMatch(测试,@\\ A \\ B [0-9A-FA-F] + \\ B \\ Z);
}

What is the most efficient way in C# 2.0 to check each character in a string and return true if they are all valid hexadecimal characters and false otherwise?

Example

void Test()
{
    OnlyHexInString("123ABC"); // Returns true
    OnlyHexInString("123def"); // Returns true
    OnlyHexInString("123g"); // Returns false
}

bool OnlyHexInString(string text)
{
    // Most efficient algorithm to check each digit in C# 2.0 goes here
}

解决方案

public bool OnlyHexInString(string test)
{
    // For C-style hex notation (0xFF) you can use @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"
    return System.Text.RegularExpressions.Regex.IsMatch(test, @"\A\b[0-9a-fA-F]+\b\Z");
}

这篇关于检查字符串是否所有字符是十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆