Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#中是否有方法检查字符串是否为有效的十六进制数?_C# - Fatal编程技术网

c#中是否有方法检查字符串是否为有效的十六进制数?

c#中是否有方法检查字符串是否为有效的十六进制数?,c#,C#,我想为我的C#存储库添加一个手动十六进制输入,但我无法验证用户的输入是否是用户在文本框中输入的合法ARGB十六进制值,或者是一个垃圾十六进制数。有人对此有潜在的解决方案吗?您可以使用: 因此,我采用了一种非常低效的方法,我首先用每一个有效的ARGB十六进制代码创建一个pastebin,然后编写了以下代码: WebClient Checker = new WebClient(); string List = Checker.Downloadstring("https://pastebin

我想为我的C#存储库添加一个手动十六进制输入,但我无法验证用户的输入是否是用户在文本框中输入的合法ARGB十六进制值,或者是一个垃圾十六进制数。有人对此有潜在的解决方案吗?

您可以使用:


因此,我采用了一种非常低效的方法,我首先用每一个有效的ARGB十六进制代码创建一个pastebin,然后编写了以下代码:

WebClient Checker = new WebClient();
string List = Checker.Downloadstring("https://pastebin.com/raw/link");//link would be the pastebin
string Input = this.TextBox.Text;
if (List.Contains(Input))
{
   //submit write file code here
}
else
{
  System.Windows.Messagebox.Show("The Input Was Not Valid! Please Input A Valid ARGB Code!", "Error!");
}

这种方法最终对我有效。不建议使用它,但它能正确完成任务。

?您使用的是什么UI框架?.net framework 4.8请注意,输入不仅需要是十六进制数,还需要是有效的ARGB值。所以它的长度必须正好是8个字符,除非您希望支持a(FFFFFF=不透明白色)和/或“一位数字节”(FFF=白色)1的默认值。
0x
前缀是c的东西,我怀疑用户是否会使用它。2.如果您已经使用了
RegexOptions.IgnoreCase
,那么在模式中也没有必要使用
a-fA-F
。3.我认为这里不需要
\g
。相反,应该在开头使用
^
,在结尾使用
$
,以指示整个字符串应该与正则表达式匹配。4.argb值的长度为8位-因此应该使用
{8}
而不是
+
WebClient Checker = new WebClient();
string List = Checker.Downloadstring("https://pastebin.com/raw/link");//link would be the pastebin
string Input = this.TextBox.Text;
if (List.Contains(Input))
{
   //submit write file code here
}
else
{
  System.Windows.Messagebox.Show("The Input Was Not Valid! Please Input A Valid ARGB Code!", "Error!");
}