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

后端 未结 6 990
天命终不由人
天命终不由人 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:16

    (Disclaimer: I have no experience with Java, so if it handles regexes in ways that render this unhelpful, I apologize.)

    If you mean getting rid of the spaces preceding the M and L, try adding optional whitespace at the end

    (?<=[\\S])[\\S]+\\s*
    

    However, this may add an extra space in the case of single-letter words. This may fix that:

    (?<=[\\S])[\\S]*\\s*
    

提交回复
热议问题