How to find words(strings) with only Uppercase letters?

时间:2015-05-04 19:28:09

标签: java string uppercase

I was wondering, is there any function or way, how to select from a random text all words(strings) with only uppercase letters? To be more specific, I want to take from text all uppercase words and put them into an string array, because those uppercase words are important for me.

For example from text: "This text was just made RANDOMLY to show what I MEANT."

In string array I will have words RANDOMLY and MEANT.

And array should looks like this String[] myArray = {"RANDOMLY", "MEANT"};

The only thing I think of is that I have go trought every single letter and check if its uppercase,

if yes

  • save the letter to a string variable
  • increase value of help integer variable (int count) by one
  • and take a look at the next letter,
    • if its uppercase again, repeat this part
    • if not - move to another letter.

I think my solotion is not very effective, so can tell me your opinion about it? Or prehaps how to make it more effective?

PS: int count is there for expelling short words with 3 letters and less.

4 个答案:

答案 0 :(得分:3)

Probably easiest way to achieve it would be using regex like \b[A-Z]{4,}\b which represents

So your code could look like:

String s = "This text was just made RANDOMLY to show what I MEANT.";

Pattern p = Pattern.compile("\\b[A-Z]{4,}\\b");
Matcher m = p.matcher(s);
while (m.find()) {
    String word = m.group();
    System.out.println(word);
}

Beside printing word to console you can also store it in List<String>.

答案 1 :(得分:1)

Split your sentence by whitespace. Then you can use StringUtils.isAllUpperCase(CharSequence cs) for instance to check every single string.

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isAllUpperCase(java.lang.CharSequence)

答案 2 :(得分:1)

Use Regex to extract them. Like

public static void main(String[] args) {
        List<String> words = new ArrayList<>();
        String dataStr = "This text was just made RANDOMLY to show what I MEANT.";
        Pattern pattern = Pattern.compile("[A-Z][A-Z]+");
        Matcher matcher = pattern.matcher(dataStr);
        while (matcher.find()) {
            words.add(matcher.group());
        }

        System.out.println(words);
    }

Output:

[RANDOMLY, MEANT]

With this in future, you could just adjust search pattern to extract what ever you want.

答案 3 :(得分:0)

这是一种最少使用正则表达式的解决方案。

String s = "This text was just made RANDOMLY to show what I MEANT.";
    String[] words = s.split(" |\\.");
    ArrayList<String> result = new ArrayList<>();

    for(String word : words) {
        String wordToUpperCase = word.toUpperCase();
        if(wordToUpperCase.equals(word)) {
            result.add(word);
        }
    }

代码行:

String[] words = s.split(" |\\.");

表示字符串将由空格(&#34;&#34;)或点(&#34;。&#34;)

分割

有关为何需要破折号(逃逸)的更多信息:Java string split with "." (dot)

如果您只是按空格分割字符串,那么:

String[] words = s.split(" ");

它会留下可能令人讨厌的结果,例如&#34; MEANT。&#34;

在任何一种情况下,单词&#34; I&#34;包含在结果中。如果您不想要,请检查每个单词的长度是否大于1。