Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java:words将大写字母提取出来_Java_Regex - Fatal编程技术网

java:words将大写字母提取出来

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 =

如何使用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 = "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);