最高效的求字符串中空格的个数,在线等待! 例如字符串: String str = " dsadasd gff bgfbf da d asd "; //这样一个字符串,前面包含的空格个数不算,中间和最后的空格要算,求总个数 //这个题目后面包含了3个空格 所以应该是8个空格希望能有最高效的方法! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 算法道是写了一个.高不高效就不晓得了.public class Test{ public static void main(String[] args){ String str = " dsadasd gff bgfbf da d asd "; int count = 0; int flag = 0; for (int i = 0; i<str.length(); i++){ char c = str.charAt(i); System.out.println (c); if(c!=' ' && flag ==0){ flag = 1; }else if(c==' ' && flag !=0){ count++; } } System.out.println (count); }} 哎... 这样肯定不行的 我也是用的for+if 老大说这样效率不好阿 您看看能改成正则嘛? 一步到位的正则不会,谁有我也想学-------------------------------------------------------------------------------------------------------------/** * */package test;/** * * @author JHF Team <[email protected]> * * * @copyright 2006-2007, BestWiz(Dalian) Co.,Ltd */public class NullCount { /** * 判断字符中含有空格的个数(前面的空格不算) * * @return int * @param nu * * @author guoqiang <[email protected]> */ public static int isNull(String nu) { @SuppressWarnings("unused") String str = ""; int count = 0; for (int i = 0; i < nu.length(); i++) { if (!String.valueOf(nu.charAt(i)).matches("\\s")) { str = nu.substring(i, nu.length()); break; } } for (int j = 0; j < str.length(); j++) { if (String.valueOf(str.charAt(j)).matches("\\s")) { count++; } } return count; } /** * main主函数 * * @return * @param args * * @author guoqiang <[email protected]> */ public static void main(String[] args) { // 判断字符中含有空格的个数(前面的空格不算) System.out.println(); System.out.println("判断字符中含有空格的个数(前面的空格不算)"); int nu1 = isNull(" a b c d "); System.out.println("nu1: " + nu1); int nu2 = isNull(" abcdefg"); System.out.println("nu2: " + nu2); }}-------------------------------------------------------------------------------------------------------------- http://hi.baidu.com/jingleq/blog/item/7bc39b62b61839dce7113a54.html看看 正则的效率其实并不高其实2楼的方法就可以了 急!!!急!!提交的东西放到了svn://127.0.0.1的什么地方? 网络通讯中反序列化如何处理? 二维数组问题 关于异常程序,不太理解 把Java应用发布到没有安装jre的机器上时,所需要的文件。 如何变量的基本数据类型? 大家讨论一下自己在项目中如何对类和方法进行命名的 关于属性properties的问题,谢谢! 向web服务器取数据的问题 刚学java,有两个问题,请网友帮我一下!!!!! 帮忙! 更新数据库的方法无法执行,后台报错:并非所有变量都已关联,如何解决?
public static void main(String[] args){
String str = " dsadasd gff bgfbf da d asd ";
int count = 0;
int flag = 0;
for (int i = 0; i<str.length(); i++){
char c = str.charAt(i);
System.out.println (c);
if(c!=' ' && flag ==0){
flag = 1;
}else if(c==' ' && flag !=0){
count++;
}
}
System.out.println (count);
}
}
哎... 这样肯定不行的 我也是用的for+if 老大说这样效率不好阿 您看看能改成正则嘛?
-------------------------------------------------------------------------------------------------------------
/**
*
*/
package test;/**
*
* @author JHF Team <[email protected]>
*
*
* @copyright 2006-2007, BestWiz(Dalian) Co.,Ltd
*/
public class NullCount { /**
* 判断字符中含有空格的个数(前面的空格不算)
*
* @return int
* @param nu
*
* @author guoqiang <[email protected]>
*/
public static int isNull(String nu) { @SuppressWarnings("unused")
String str = "";
int count = 0;
for (int i = 0; i < nu.length(); i++) {
if (!String.valueOf(nu.charAt(i)).matches("\\s")) {
str = nu.substring(i, nu.length());
break;
}
}
for (int j = 0; j < str.length(); j++) {
if (String.valueOf(str.charAt(j)).matches("\\s")) {
count++;
}
}
return count;
} /**
* main主函数
*
* @return
* @param args
*
* @author guoqiang <[email protected]>
*/
public static void main(String[] args) { // 判断字符中含有空格的个数(前面的空格不算)
System.out.println();
System.out.println("判断字符中含有空格的个数(前面的空格不算)");
int nu1 = isNull(" a b c d ");
System.out.println("nu1: " + nu1);
int nu2 = isNull(" abcdefg");
System.out.println("nu2: " + nu2);
}
}
--------------------------------------------------------------------------------------------------------------
看看 正则的效率其实并不高
其实2楼的方法就可以了