Get the first letter of each word in a string using regex

后端 未结 6 939
天命终不由人
天命终不由人 2021-01-13 04:10

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[]         


        
6条回答
  •  悲哀的现实
    2021-01-13 04:17

    It's not fixing the regex, but adding a .trim() to the output string still works:

    String name = "First Middle Last";
    for(String s : name.split("(?<=[\\S])[\\S]+")) System.out.println(s.trim());
    

    output:

    F
    M
    L
    

提交回复
热议问题