假设我有这个字符串:
String helloWorld = "One,Two,Three,Four!";
如何计算 String helloWorld
中逗号的数量?
最佳答案
最简单的方法是遍历字符串并计算它们。
int commas = 0;
for(int i = 0; i < helloWorld.length(); i++) {
if(helloWorld.charAt(i) == ',') commas++;
}
System.out.println(helloWorld + " has " + commas + " commas!");
关于Java - 计算字符串中符号的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5098429/