java语言怎样实现计算字符串有几个0开头?

请教个问题:怎样计算出一个字符串开头有几个0比如str1="0315";得出1str2="000315";得出3如果是str3="3105";0不属于开头所以得出0谢谢... 请教个问题:
怎样计算出一个字符串开头有几个0

比如 str1="0315"; 得出1
str2="000315"; 得出3

如果是 str3="3105"; 0不属于开头所以得出0
谢谢
展开
 我来答
百度网友37397c621
2013-06-25 · TA获得超过695个赞
知道小有建树答主
回答量:427
采纳率:100%
帮助的人:153万
展开全部
public static void main(String[] args) {
String str = "000305";
Pattern p = Pattern.compile("^0*");
Matcher m = p.matcher(str);
int count = 0;
while (m.find()) {
count = m.group().length();
}
System.out.println("有" + count + "个0开头......");
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
雨寒no泪
2013-06-25 · 超过31用户采纳过TA的回答
知道答主
回答量:159
采纳率:100%
帮助的人:85万
展开全部
package com.egoo.endWith;

public class StartWith
{
public static void main(String[] args)
{
String s = "000123";
jisuan(s);

}

public static int jisuan(String s){
int num = 0;

while(s.startsWith ( "0" )){
s = s.substring ( 1, s.length ()-1 );
System.out.println (s);
num++;
}
System.out.println (num);
return num;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
中校的家
2013-07-03
知道答主
回答量:32
采纳率:0%
帮助的人:19.3万
展开全部
public static void main(String[] args) {

String str = "00 000001123 2123";

String strs[] = str.split("0");
int a = 0;
for (int i = 0; i < strs.length; i++) {
if (!strs[i].equals("") && strs[i] != "") {
break;
}
a += 1;
System.out.println(strs[i]);
}
System.out.println(strs.length);
System.out.println(a);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
如何不懒惰
2013-06-27
知道答主
回答量:11
采纳率:0%
帮助的人:5.7万
展开全部
public static void main(String[] args){
String str = "000123";
char[] ch = str.toCharArray();
int num=0;
for (int i = 0; i < ch.length; i++) {
    if (ch[i]=='0') {
                num++;
}else{
                break;
            }
}
System.out.println(num);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zhou073
2013-06-25 · TA获得超过134个赞
知道小有建树答主
回答量:191
采纳率:80%
帮助的人:66万
展开全部
public class End0 {//用递归做
public static void main(String[] args) {
String s = "0003630";
int i = start0(s);
System.out.println(i);
}
public static int start0(String s){
return s.startsWith("0") ? 1 + start0(s.substring(1)) : 0;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式