我是使用java的新手。我想问一下,如果我有一个文本文件,每行包含不同的单词,我想将该文件作为字符串读取,以便检测是否有某些单词是用全部大写(缩写)写的。例外情况是,如果单词以“#”或“@”开头,它将忽略计数。例如,我有:
OMG特里很可爱#HAWT SMH结果将是: 缩写= 2。
或
特里喜欢TGIF派对@ANDERSON
结果将是: 缩写= 1。
请帮忙
答案 0 :(得分:0)
尝试使用 .split(String T)方法和 .contains(char C)方法..... 我想他们会帮助你很多....
功能拆分:
http://www.tutorialspoint.com/java/java_string_split.htm
功能包含:
答案 1 :(得分:0)
String str1 = "OMG terry is cute #HAWT SMH";
String str2 = "terry likes TGIF parties @ANDERSON";
Pattern p = Pattern.compile("(?>\\s)([A-Z]+)(?=\\s)");
Matcher matcher = p.matcher(" "+str1+" ");//pay attention! adding spaces
// before and after to catch potentials in
// beginning/end of the sentence
int i=0;
while (matcher.find()) {
i++; //count how many matches were found
}
System.out.println("matches: "+i); // prints 2
matcher = p.matcher(" "+str2+" ");
i=0;
while (matcher.find()) {
i++;
}
System.out.println("matches: "+i); // prints 1
<强>输出:强>
matches: 2
matches: 1
答案 2 :(得分:0)
这是你的蜘蛛问题的火箭筒。
(mystring+" ").split("(?<!@|#)[A-Z]{2,}").length-1;
示例:
mystring = "OMG terry is cute #HAWT SMH";
String[] arr = (mystring+" ").split("(?<!@|#)[A-Z]{2,}").length-1;
//arr is now {"", " terry is cute #HAWT ", " "}, three strings
return arr.length-1; //returns 2