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"
"Determines whether the beginning of this string instance matches a specified string."
string numbers = "000445454";
if (numbers.StartsWith("0"))
//Do Something
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'
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.
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";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.