WAP to Split a String on Uppercase Characters

Split a String on Uppercase Characters: The given Java program takes an input string abCdefGHijkl and splits it into substrings based on the occurrence of uppercase letters. The resulting output separates the substrings wherever an uppercase letter is found, adding spaces between them. The output for the given input is ab Cdef G Hijkl.

Split a String on Uppercase Characters

package com.softwaretestingo.interviewprograms;
import java.util.Arrays;
public class InterviewPrograms42 
{
	/*
	 * Input =“abCdefGHijkl”; 
	 * Output: “ab Cdef G Hijkl”
	 */
	public static void main(String[] args) 
	{
		String st = "abCdefGHijkl";
		String[] r = st.split("(?=\\p{Upper})");
		Arrays.stream(r).forEach(System.out::println);
	}
}

Output

ab
Cdef
G
Hijkl

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment