C# 中的字符串值验证

发布于 2024-11-15 04:21:57 字数 840 浏览 3 评论 0原文

有两个变量被赋值为“003”和“00 3”。并将其转换为 byte[] 如下。

之前:

myStr1 = "003"; // valid, there is no blank inserted.
myStr2 = "00 3"; // invalid, there is one blank character or multi blanks character inserted.

通过convert()转换后,如果发现有空白字符,源字符串将转换为字节数组。

myVal1 = "3";  // valid after convert
myVal2[0] = 0; // invalid, since the source myStr2 is invalid.
myVal2[1] = 1; // same as above.

现在我需要根据转换后的结果确定源字符串有效或无效。我不知道怎么说结果是一个字节数组。您能给我一些建议吗?提前致谢。

输入字符串类型源值作为SourVal

if (ResultVal is Byte Array) // how to translate the statement to C# code?
   SourVal is Invalid;
else if (ResultVal is still String type) // how to translate the statement to C# code?
   SourVal is valid;

ps:我在实践中没有尝试过typeof()和gettype()方法。我不知道如何使用这些方法。或者还有其他更好的方法来进行我的验证。

There is two variable was assigned the value of "003" and "00 3". And it was convert to byte[] as below.

Before:

myStr1 = "003"; // valid, there is no blank inserted.
myStr2 = "00 3"; // invalid, there is one blank character or multi blanks character inserted.

After converted by convert(), if there are blank characters found, the source string will be convert to byte array.

myVal1 = "3";  // valid after convert
myVal2[0] = 0; // invalid, since the source myStr2 is invalid.
myVal2[1] = 1; // same as above.

And now I need determine the source string is valid or invalid based on the converted result. I dont' know how to say the result is an byte array. Could you please give me some advice. Thanks in advance.

input string type source value as SourVal

if (ResultVal is Byte Array) // how to translate the statement to C# code?
   SourVal is Invalid;
else if (ResultVal is still String type) // how to translate the statement to C# code?
   SourVal is valid;

ps: I failed to try the methods of typeof() and gettype() at my practice. I don't know how to use the methods. Or there is other better method for my validation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甲如呢乙后呢 2024-11-22 04:21:57

也许使用:

        if (ResultVal is byte[]) {
           // SourVal is Invalid;
        } else if ( ResultVal is String ) { 
            //SourVal is valid;
        }

maybe use:

        if (ResultVal is byte[]) {
           // SourVal is Invalid;
        } else if ( ResultVal is String ) { 
            //SourVal is valid;
        }
灯下孤影 2024-11-22 04:21:57

尝试使用 IsWhiteSpace

Try using IsWhiteSpace

毁梦 2024-11-22 04:21:57
//Check the String for Blank spaces if found then don't convert

if(!str.Contains(" "))

{

//use the convert method 

}
else

{

//Write Message for an Invalid String

}
//Check the String for Blank spaces if found then don't convert

if(!str.Contains(" "))

{

//use the convert method 

}
else

{

//Write Message for an Invalid String

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文