• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP to Split a String on Uppercase Characters

WAP to Split a String on Uppercase Characters

Last Updated on: July 26, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • 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

Here’s a step-by-step explanation of the program:

  1. Define the main method:
    • The program starts executing from the main method.
  2. Initialize the input string:
    • The input string st is initialized with the value “abCdefGHijkl”.
  3. Split the string based on uppercase letters:
    • The split method is used on the input string st, with the regex pattern “(?=\\p{Upper})”.
    • The regex pattern “(?=\\p{Upper})” is a positive lookahead, which matches an empty string that is followed by an uppercase letter. This effectively splits the string at the positions before each uppercase letter.
    • The result of the split method is an array of substrings, which are stored in the variable r.
  4. Print the output:
    • The program uses the Arrays.stream() method to convert the array r into a stream of substrings.
    • The forEach method is used to iterate through each substring in the stream and print it using System.out::println.

For the given input “abCdefGHijkl”, the program splits the string at the positions before uppercase letters ‘C’, ‘G’, and ‘H’, resulting in substrings “ab “, “Cdef “, “G”, “Hijkl”. The program then prints each substring on a new line, with spaces separating the substrings wherever an uppercase letter was found.

    Leap Year In Java
    LeapYear In Java With Example Program
    Swap Two Numbers In Java
    Swap Two Numbers In Java
    WAP For Sum Of Digits Of a String
    WAP For Sum Of Digits Of a String
    WAP to Find all the Permutations of a String
    WAP to Find all the Permutations of a String
    Mountain Array
    WAP to Validate Array Is a Mountain Array & Peak...
    WAP to Swap Two Adjacent Elements in an Array
    WAP to Swap Two Adjacent Elements in an Array
    WAP to Format a Phone Number in Human-Readable View
    WAP to Format a Phone Number in Human-Readable View
    Java Program Interview Reverse Characters Only From String
    WAP to Reverse Characters Only From String
    WAP to Sort Array Of String According To String Length
    WAP to Sort Array Of String According To String Length
    WAP to Print Highest ASCII Value Character From String
    WAP to Print Highest ASCII Value Character From String

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers