Examples:
Input : Geeks for geeks Output :Gfg Input : United Kingdom Output : UK
\b[a-zA-Z]
// Java program to demonstrate extracting first // letter of each word using Regex import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { static void printFirst(String s) { Pattern p = Pattern.compile("\\b[a-zA-Z]"); Matcher m = p.matcher(s); while (m.find()) System.out.print(m.group()); System.out.println(); } public static void main(String[] args) { String s1 = "Geeks for Geeks"; String s2 = "A Computer Science Portal for Geeks"; printFirst(s1); printFirst(s2); } }
Output:
GfG ACSPfG
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.