
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Print first letter of each word using regex
In this article, we will understand how to print first letter of each word using regex. A regular expression is a sequence of characters that forms a search pattern. A regular expression can be a single character, or a more complicated pattern.
A regular expression helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.
Below is a demonstration of the same −
Suppose our input is −
Input String_1: Java Program Input String_2: Joy of learning
The desired output would be −
Result_1: JP Result_2: Jol
Algorithm
Step 1 - START Step 2 - Declare two string values namely input_string_1 and input_string_2. Declare a regex Pattern namely string_pattern and a Matcher object namely string_matcher. Step 3 - Define the values. Step 4 - Using a while-loop, compute string_matcher.group() to fetch the first letter of each word. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regex { public static void main(String[] args) { System.out.println("Required packages have been imported"); String input_string_1 = "Java Program"; System.out.println("\nThe first string is defined as: " +input_string_1); Pattern string_pattern = Pattern.compile("\b[a-zA-Z]"); Matcher string_matcher = string_pattern.matcher(input_string_1); System.out.println("The first letters of the string is : "); while (string_matcher.find()) System.out.print(string_matcher.group()); System.out.println(); String input_string_2 = "Joy of learning"; System.out.println("\nThe second string is defined as: " +input_string_2); Matcher string_matcher_2 = string_pattern.matcher(input_string_2); System.out.println("The first letters of the string is : "); while (string_matcher_2.find()) System.out.print(string_matcher_2.group()); System.out.println(); } }
Output
Required packages have been imported The first string is defined as: Java Program The first letters of the string is : JP The second string is defined as: Java Program The first letters of the string is : Jol
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regex { static void print_regex_string(String s) { Pattern string_pattern = Pattern.compile("\b[a-zA-Z]"); Matcher string_matcher = string_pattern.matcher(s); System.out.println("The first letters of the string is : "); while (string_matcher.find()) System.out.print(string_matcher.group()); System.out.println(); } public static void main(String[] args) { System.out.println("Required packages have been imported"); String input_string_1 = "Java Program"; System.out.println("\nThe first string is defined as: " +input_string_1); print_regex_string(input_string_1); String input_string_2 = "Joy of learning"; System.out.println("\nThe second string is defined as: " +input_string_1); print_regex_string(input_string_2); } }
Output
Required packages have been imported The first string is defined as: Java Program The first letters of the string is : JP The second string is defined as: Java Program The first letters of the string is : Jol
- Related Articles
- C++ Program to Print first letter of each word using regex
- Golang program to print first letter of each word using regex
- Print first letter of each word in a string using C# regex
- Getting first letter of each word in a String using regex in Java
- Print first letter of each word in a string in C#
- How to get first letter of each word using regular expression in Java?
- Python program to capitalize each word's first letter
- Capitalizing first letter of each word JavaScript
- How to capitalize the first letter of each word in a string using JavaScript?
- Capitalize last letter and Lowercase first letter of a word in Java
- Extracting each word from a string using Regex in Java
- How to print the first character of each word in a String in Java?
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?
- Java Program to Capitalize the first character of each word in a String

Advertisements