How do I count the number of occurrences of a char

2018-12-31 01:46发布

I have the string

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).

标签: java string
30条回答
余欢
2楼-- · 2018-12-31 02:26
import java.util.Scanner;

class apples {

    public static void main(String args[]) {    
        Scanner bucky = new Scanner(System.in);
        String hello = bucky.nextLine();
        int charCount = hello.length() - hello.replaceAll("e", "").length();
        System.out.println(charCount);
    }
}//      COUNTS NUMBER OF "e" CHAR´s within any string input
查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 02:28

A shorter example is

String text = "a.b.c.d";
int count = text.split("\\.",-1).length-1;
查看更多
深知你不懂我心
4楼-- · 2018-12-31 02:29

I had an idea similar to Mladen, but the opposite...

String s = "a.b.c.d";
int charCount = s.replaceAll("[^.]", "").length();
println(charCount);
查看更多
谁念西风独自凉
5楼-- · 2018-12-31 02:29

I don't like the idea of allocating a new string for this purpose. And as the string already has a char array in the back where it stores it's value, String.charAt() is practically free.

for(int i=0;i<s.length();num+=(s.charAt(i++)==delim?1:0))

does the trick, without additional allocations that need collection, in 1 line or less, with only J2SE.

查看更多
梦该遗忘
6楼-- · 2018-12-31 02:30
int count = (line.length() - line.replace("str", "").length())/"str".length();
查看更多
骚的不知所云
7楼-- · 2018-12-31 02:31

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

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

1) Using Apache Commons

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

2) Using Spring Framework's

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

3) Using replace

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

4) Using replaceAll (case 1)

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

5) Using replaceAll (case 2)

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

6) Using split

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

7) Using Java8 (case 1)

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

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) Using StringTokenizer

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

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

More info in github

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
查看更多
登录 后发表回答