Reputation: 1251
I'm currently trying to detect if a number has a leading zero. Not sure what the best way is to detect that. An example would something like this "000445454"
Upvotes: 2
Views: 7180
Reputation: 623
I am not familiar with c# syntax
But in C++ in order to find 00, 05, 01 , i tend to do this:
We can find the index of first zero from left using
index = str.find('0');
And now check if index == 0 means leading ZERO and the length of string is greater than 1
if(index ==0 and str.length()>1){
cout<<"Leading Zero";
}
Upvotes: 0
Reputation: 1959
string str = "0";
if(str.StartsWith("0") && str.Length !=1) Console.WriteLine("false");
else Console.WriteLine("true");
if the string starts with '0'
and if it has more than one character.
Upvotes: 0
Reputation: 11
One easy way to detect a leading zero in a non empty "s" string is to compare its first element with the character '0':
s[0] == '0'
Upvotes: 1
Reputation: 14153
"Determines whether the beginning of this string instance matches a specified string."
string numbers = "000445454";
if (numbers.StartsWith("0"))
//Do Something
Upvotes: 11