s.length() - s.replaceAll(" ", "").length()
returns you number of spaces.
There are more ways. For example"
int spaceCount = 0;
for (char c : str.toCharArray()) {
if (c == ' ') {
spaceCount++;
}
}
etc., etc.
In your case you tried to split string using
- TAB. You will get right result if you use " "
instead. Using s
may be confusing since it matches all whitepsaces - regular spaces and TABs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…