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
int count
) by one 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.
答案 0 :(得分:3)
Probably easiest way to achieve it would be using regex like \b[A-Z]{4,}\b
which represents
\b
word boundary - place between alphanumeric and non-alphanumeric characters[A-Z]
character in range A-Z
{4,}
which appears at least 4 times (if we don't want single letter words like I
to be counted) (more info at: http://www.regular-expressions.info/repeat.html)\b
another word boundary to make sure that we are reading entire wordSo 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.
答案 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。