开发者

string value validation in C#

There is two variable was assig开发者_如何学JAVAned 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.


maybe use:

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


Try using IsWhiteSpace


//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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜