I’m trying to get the first letter of each word in a string using regex, here is what I have tried:
public class Test
{
public static void main(String[] args)
{
String name = "First Middle Last";
for(String s : name.split("(?<=[\\S])[\\S]+")) System.out.println(s);
}
}
The output is as follows:
F
M
L
How can I fix the regex to get the correct output?
Edit Took some suggestions in the comments, but kept the
\Sbecause\wis only alpha-numeric and might break unexpectedly on any other symbols.Fixing the regex and still using split: