检测字符串中的前导零
Detect Leading Zero in String
我目前正在尝试检测数字是否有前导零。不确定检测到它的最佳方法是什么。一个例子是这样的“000445454”
"Determines whether the beginning of this string instance matches a specified string."
string numbers = "000445454";
if (numbers.StartsWith("0"))
//Do Something
检测非空 "s" 字符串中前导零的一种简单方法是将其第一个元素与字符“0”进行比较:
s[0] == '0'
string str = "0";
if(str.StartsWith("0") && str.Length !=1) Console.WriteLine("false");
else Console.WriteLine("true");
如果字符串以 '0'
开头并且有多个字符。
我不熟悉c#语法
但是在 C++ 中为了找到 00, 05, 01 ,我倾向于这样做:
我们可以使用
从左边找到第一个零的索引
index = str.find('0');
现在检查 index == 0 是否表示前导零且字符串长度大于 1
if(index ==0 and str.length()>1){
cout<<"Leading Zero";
}
我目前正在尝试检测数字是否有前导零。不确定检测到它的最佳方法是什么。一个例子是这样的“000445454”
"Determines whether the beginning of this string instance matches a specified string."
string numbers = "000445454";
if (numbers.StartsWith("0"))
//Do Something
检测非空 "s" 字符串中前导零的一种简单方法是将其第一个元素与字符“0”进行比较:
s[0] == '0'
string str = "0";
if(str.StartsWith("0") && str.Length !=1) Console.WriteLine("false");
else Console.WriteLine("true");
如果字符串以 '0'
开头并且有多个字符。
我不熟悉c#语法
但是在 C++ 中为了找到 00, 05, 01 ,我倾向于这样做:
我们可以使用
从左边找到第一个零的索引index = str.find('0');
现在检查 index == 0 是否表示前导零且字符串长度大于 1
if(index ==0 and str.length()>1){
cout<<"Leading Zero";
}