java:单词提取大写字母

hey*_*man 2 java regex

如何使用java从单词中提取大写字母?

example:
enter your words:Hello I Am Heyman
output:HIAH
Run Code Online (Sandbox Code Playgroud)

谢谢

Pet*_*rey 8

你可以尝试replaceAll

String text2 = text.replaceAll("[^A-Z]", "");
Run Code Online (Sandbox Code Playgroud)

作为@Vic评论,包括所有英文/非英文大写字母.

String text2 = text.replaceAll("[^\p{Lu}]", "");
Run Code Online (Sandbox Code Playgroud)

  • OP应首先尝试自己. (3认同)
  • 很好,但我会使用String text2 = text.replaceAll("[^\p {Lu}]",""); 确保它也适用于非英语Unicode字母. (2认同)