如何计算java字符串中的空格?
我需要计算我的字符串中的空格数,但是当我运行它时,我的代码给了我一个错误的数字,出了什么问题?
int count=0; String arr[]=s.split("\t"); OOPHelper.println("Number of spaces are: "+arr.length); count++;
s.length() - s.replaceAll(" ", "").length()
返回空格数。
有更多的方法。 例如”
int spaceCount = 0; for (char c : str.toCharArray()) { if (c == ' ') { spaceCount++; } }
等等
在您的情况下,您尝试使用\t
– TAB拆分字符串。 如果您使用" "
您将获得正确的结果。 使用\s
可能会造成混淆,因为它匹配所有whitepsaces – 常规空间和TAB。
这是一种不同的方式,它是一个简单的单行:
int spaces = s.replaceAll("[^ ]", "").length();
这可以通过有效地删除所有非空格然后获取剩余的空间(空格)来实现。
您可能想要添加空检查:
int spaces = s == null ? 0 : s.replaceAll("[^ ]", "").length();
Java 8更新
您也可以使用流:
int spaces = s.chars().filter(c -> c == (int)' ').count();
\t
将匹配制表符,而不是空格,也应该用双斜杠引用: \\t
。 您可以调用s.split( " " )
但不会计算连续的空格。 我的意思是……
String bar = " ba jfjf jjj j "; String[] split = bar.split( " " ); System.out.println( split.length ); // Returns 5
因此,尽管有七个空格字符,但只有五个空间块。 我猜,这取决于你想要计算的数量。
Commons Lang是你的朋友。
int count = StringUtils.countMatches( inputString, " " );
最快的方法是:
int count = 0; for(int i = 0; i < str.length(); i++) { if(Character.isWhitespace(str.charAt(i))) count++; }
这将捕获所有被认为是空格的字符。
正则表达式解决方案需要编译正则表达式并使用它 - 需要大量的开销。 获取字符数组需要分配。 迭代字节数组会更快,但前提是你确定你的字符是ASCII。
您的代码将计算选项卡的数量,而不是空格的数量。 此外,选项卡的数量将比arr.length
少一个。
另一种使用正则表达式
int length = text.replaceAll("[^ ]", "").length();
如果您使用Java 8,则以下内容应该有效:
long count = "0 1 2 3 4.".chars().filter(Character::isWhitespace).count();
这也可以在Java 8中使用Eclipse Collections :
int count = CharAdapter.adapt("0 1 2 3 4.").count(Character::isWhitespace);
注意:我是Eclipse Collections的提交者。
您提供的代码将打印选项卡的数量,而不是空格的数量。 以下函数应计算给定字符串中的空白字符数。
int countSpaces(String string) { int spaces = 0; for(int i = 0; i < string.length(); i++) { spaces += (Character.isWhitespace(string.charAt(i))) ? 1 : 0; } return spaces; }
使用java.util.regex.Pattern / java.util.regex.Matcher的解决方案
String test = "foo bar baz "; Pattern pattern = Pattern.compile(" "); Matcher matcher = pattern.matcher(test); int count = 0; while (matcher.find()) { count++; } System.out.println(count);
请检查以下代码,它可以提供帮助
public class CountSpace { public static void main(String[] args) { String word = "SN PRASAD RAO"; String data[];int k=0; data=word.split(""); for(int i=0;i
我只需要做类似的事情,这就是我用过的东西:
String string = stringValue; String[] stringArray = string.split("\\s+"); int length = stringArray.length; System.out.println("The number of parts is: " + length);
public static void main(String[] args) { String str = "Honey dfd tEch Solution"; String[] arr = str.split(" "); System.out.println(arr.length); int count = 0; for (int i = 0; i < arr.length; i++) { if (!arr[i].trim().isEmpty()) { System.out.println(arr[i]); count++; } } System.out.println(count); }
public static void main(String[] args) { Scanner input= new Scanner(System.in);` String data=input.nextLine(); int cnt=0; System.out.println(data); for(int i=0;i
这个程序肯定会帮助你。
class SpaceCount { public static int spaceCount(String s) { int a=0; char ch[]= new char[s.length()]; for(int i = 0; i < s.length(); i++) { ch[i]= s.charAt(i); if( ch[i]==' ' ) a++; } return a; } public static void main(String... s) { int m = spaceCount("Hello I am a Java Developer"); System.out.println("The number of words in the String are : "+m); } }
最精确,最准确,最快的方法是:
String Name="Infinity War is a good movie"; int count =0; for(int i=0;i
计算空间的简单而快速的方法
String fav="foo hello me hi"; for( int i=0; i