如何使用java从单词中提取大写字母?
example:
enter your words:Hello I Am Heyman
output:HIAH
感谢
你可以试试replaceAll
String text2 = text.replaceAll("[^A-Z]", "");
正如@Vic 评论,包括所有英文/非英文大写字母。
String text2 = text.replaceAll("[^\p{Lu}]", "");
这是在 String 上带有 for 循环的解决方案:
String myString = "Hello I Am Heyman";
String outPutString = "";
for(int i = 0; i < myString.length(); i++) {
char c = myString.charAt(i);
if (Character.isUpperCase(c))
{
// it is Capital Letter
outPutString += c;
}
}
System.out.println(outPutString);
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(textToLookInto);
String outString="";
while(m.find()){
outString+=m.group();
}
System.out.println(outString);
由于您已标记为正则表达式,因此考虑添加仅正则表达式的解决方案。