java:words将大写字母提取出来
如何使用java从单词中提取大写字母?java:words将大写字母提取出来,java,regex,Java,Regex,如何使用java从单词中提取大写字母? example: enter your words:Hello I Am Heyman output:HIAH 谢谢您可以全部试用 String text2 = text.replaceAll("[^A-Z]", ""); As@Vic注释,包括所有英文/非英文大写字母 String text2 = text.replaceAll("[^\p{Lu}]", ""); 以下是字符串上带有for循环的解决方案: String myString =
example:
enter your words:Hello I Am Heyman
output:HIAH
谢谢您可以全部试用
String text2 = text.replaceAll("[^A-Z]", "");
As@Vic注释,包括所有英文/非英文大写字母
String text2 = text.replaceAll("[^\p{Lu}]", "");
以下是字符串上带有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);
String myString=“你好,我是海曼”;
字符串outPutString=“”;
对于(int i=0;i
因为您已经标记为regex,所以考虑添加一个只包含regex的解决方案 让我们先看看你的尝试怎么样?OP应该先自己尝试。很好,但我会使用字符串text2=text.replaceAll(“[^\p{Lu}]”,“”);为了确保它不适用于任何英文Unicode字母。outString必须是StringBuilder(当TextToLookin变大时速度更快)
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(textToLookInto);
String outString="";
while(m.find()){
outString+=m.group();
}
System.out.println(outString);