Answers for "count characters of a string java"

3

count the number of words in a string java

public static void main(String[] args)
    { 
        //return the number of words in a string 
        
       String example = "This is a good exercise"; 
       
       int length = example.split(" ").length;
       
       System.out.println("The string is " + length + " words long.");
        
        
    }
Posted by: Guest on June-04-2020
2

counting the number of characters in a string java

public static void main(String[] args) {
		int wordCount = 0;
		String word = "hill";
		for (char letter = 'a'; letter <= 'z'; letter++) {
			for (int i = 0; i < word.length(); i++) {
				if (word.charAt(i) == letter) {
					wordCount++;
				}
			}
			if (wordCount > 0) {
				System.out.println(letter + "=" + wordCount);
				wordCount = 0;
			}
		}
	}
Posted by: Guest on January-23-2020
1

count occurrences of character in string java 8

String someString = "elephant";
long count = someString.chars().filter(ch -> ch == 'e').count();
assertEquals(2, count);
 
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
assertEquals(2, count2);
Posted by: Guest on June-17-2020

Code answers related to "count characters of a string java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language