Java- Find all Capital Letters in the String


In this short tutorial, we’ll find all the capital letters in the given String using Java. Character.isUpperCase(char ch) returns true if the given character is a capital letter.

For example, we have an employee name i.e “juHi GuPta”, in this string H, G, and P is the capital letter.

FindCapitalLetter.java
package org.websparrow;

import java.util.function.Predicate;

public class FindCapitalLetter {

	public static void main(String[] args) {

		findCapitalUsingPredicate("ManisH fartIyaL");

		findCapital("juHi GuPta");

	}

	// using java functional interface Predicate
	private static void findCapitalUsingPredicate(final String name) {
		System.out.print("Capita Letters in " + name + ": ");
		
		Predicate<Character> predicate = Character::isUpperCase;

		for (int i = 0; i < name.length(); i++) {

			if (predicate.test(name.charAt(i))) {
				System.out.print(name.charAt(i));
			}
		}
		System.out.println();
	}

	private static void findCapital(final String name) {
		System.out.print("Capita Letters in " + name + ": ");
		
		for (int i = 0; i < name.length(); i++) {

			if (Character.isUpperCase(name.charAt(i))) {
				System.out.print(name.charAt(i));
			}
		}
	}

}

Output:

Capita Letters in ManisH fartIyaL: MHIL
Capita Letters in juHi GuPta: HGP

References

  1. How to sort Map by Key or Value in Java 8
  2. How to sort list in Java 8

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.