[ How to identify the string should not starts with S and V ]
Hi i want check the string should not starts with V and S. I am using the below code to findout. But it always go to the error section.Please help me to do this.
if (!Smc.StartsWith("V") || !Smc.StartsWith("S"))
{
RetStr = "1:Invalid String";
return RetStr;
}
Answer 1
If you want to go to your error section when the string starts with V or S, you can do this..
if ( Smc.StartsWith("V") || Smc.StartsWith("S"))
{
RetStr = "1:Invalid String";
return RetStr;
}
Answer 2
Right now, your if
condition is essentially:
"If Smc doesn't start with V, or if Smc doesn't start with S"
Since a string can't start with both V and S at the same time, at least one of those statements is always true.
You'll want to remove the not
operator, like so:
if (Smc.StartsWith("V") || Smc.StartsWith("S"))
{
RetStr = "1:Invalid String";
return RetStr;
}
Answer 3
The IF Statement:
if (!Smc.StartsWith("V") || !Smc.StartsWith("S"))
is interpreted as is smc doesn't start with "V" OR smc doesn't start with "S"
If smc starts with S, then it doesn't start with V. so the first condition is true. If smc starts with V, then it doesn't start with S, so the second condition is true. IF smc starts with any other character, both conditions are true.
Therefore it always executes the return block.