如何计算字符串中字符的出现次数? [英] How do I count the number of occurrences of a char in a String?

查看:42
本文介绍了如何计算字符串中字符的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串

a.b.c.d

我想计算 '.' 的出现次数以惯用的方式,最好是单行.

I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner.

(之前我曾将此约束表示为没有循环",以防您想知道为什么每个人都试图不使用循环来回答).

(Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop).

推荐答案

总结其他答案以及我所知道的所有使用单线完成此操作的方法:

Summarize other answer and what I know all ways to do this using a one-liner:

   String testString = "a.b.c.d";

1) 使用 Apache Commons

int apache = StringUtils.countMatches(testString, ".");
System.out.println("apache = " + apache);

2) 使用 Spring 框架的

int spring = org.springframework.util.StringUtils.countOccurrencesOf(testString, ".");
System.out.println("spring = " + spring);

3) 使用替换

int replace = testString.length() - testString.replace(".", "").length();
System.out.println("replace = " + replace);

4) 使用 replaceAll(案例 1)

4) Using replaceAll (case 1)

int replaceAll = testString.replaceAll("[^.]", "").length();
System.out.println("replaceAll = " + replaceAll);

5) 使用 replaceAll(情况 2)

5) Using replaceAll (case 2)

int replaceAllCase2 = testString.length() - testString.replaceAll("\\.", "").length();
System.out.println("replaceAll (second case) = " + replaceAllCase2);

6) 使用 split

int split = testString.split("\\.",-1).length-1;
System.out.println("split = " + split);

7) 使用 Java8(案例 1)

7) Using Java8 (case 1)

long java8 = testString.chars().filter(ch -> ch =='.').count();
System.out.println("java8 = " + java8);

8) 使用 Java8(案例 2),对于 unicode 来说可能比案例 1 更好

8) Using Java8 (case 2), may be better for unicode than case 1

long java8Case2 = testString.codePoints().filter(ch -> ch =='.').count();
System.out.println("java8 (second case) = " + java8Case2);

9) 使用 StringTokenizer

int stringTokenizer = new StringTokenizer(" " +testString + " ", ".").countTokens()-1;
System.out.println("stringTokenizer = " + stringTokenizer);

来自评论:注意 StringTokenizer,对于 abcd 它将起作用,但是对于 a...bc...d 或 ...abcd 或 a....b......c...d... 或等等.它不会工作.它只是将计数.字符之间只有一次

From comment: Be carefull for the StringTokenizer, for a.b.c.d it will work but for a...b.c....d or ...a.b.c.d or a....b......c.....d... or etc. it will not work. It just will count for . between characters just once

更多信息在 github

性能测试(使用JMH,模式=AverageTime,得分0.0100.351 好):

Perfomance test (using JMH, mode = AverageTime, score 0.010 better then 0.351):

Benchmark              Mode  Cnt  Score    Error  Units
1. countMatches        avgt    5  0.010 ±  0.001  us/op
2. countOccurrencesOf  avgt    5  0.010 ±  0.001  us/op
3. stringTokenizer     avgt    5  0.028 ±  0.002  us/op
4. java8_1             avgt    5  0.077 ±  0.005  us/op
5. java8_2             avgt    5  0.078 ±  0.003  us/op
6. split               avgt    5  0.137 ±  0.009  us/op
7. replaceAll_2        avgt    5  0.302 ±  0.047  us/op
8. replace             avgt    5  0.303 ±  0.034  us/op
9. replaceAll_1        avgt    5  0.351 ±  0.045  us/op

这篇关于如何计算字符串中字符的出现次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆