Java - 计算字符串中符号的数量

标签 java

假设我有这个字符串:

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/

相关文章:

java - 从数据库中删除时,ResultSet 关闭后不允许进行操作

java - 如何对发送到 Activity 的不同 Extras 执行不同的操作?

java - 如何编写 value() 方法?

java - 如何将数字舍入到最接近的 64 的倍数

java - GWT - 从 context.xml 加载配置项

java - linux进程执行问题

java - Joda Time : First day in this year's ISO week 1

java - 检测要显示的图像大小并通过延迟加载图像使用它

java - JCheckBox:多行文本的垂直对齐

java - 动态操作应用程序属性 - 保险库设置(或任何其他值)