我想计算字符串内容中的单词和行数。 这是我的代码:
private int[] getLineAndWordCount(final String textContent) {
int wordCount = 0;
int lineCount = 0;
if (textContent.length() > 0) {
textContent = textContent.replace("\t", " ");
String[] newLineArrays = textContent.split("\n");
lineCount = newLineArrays.length;
for (String newLineStr : newLineArrays) {
String[] wordsArray = newLineStr.trim().split(" ");
for (String word : wordsArray) {
if (word.length() > 0) {
wordCount++;
}
}
}
}
return new int[]{lineCount, wordCount};
}
此代码工作正常,但在exution期间,它将创建如此多的subStrings。那么有没有其他有效的方法来做同样的事情。感谢。
答案 0 :(得分:2)
尝试使用java.util.Scanner
。例如:
Scanner textScanner = new Scanner(text);
while (textScanner.hasNextLine()) {
linesCount++;
Scanner wordsScanner = new Scanner(textScanner.nextLine());
while (wordsScanner.hasNext()) {
wordsCount++;
wordsScanner.next();
}
}
java.util.Scanner
的javadoc:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
答案 1 :(得分:0)
你可以这样试试。
Scanner scanner=new Scanner(new File("Location"));
int numberOfLines=0;
StringTokenizer stringTokenizer=null;
int numberOfWords=0;
while (scanner.hasNextLine()){
stringTokenizer=new StringTokenizer(scanner.nextLine()," ");
numberOfWords=numberOfWords+stringTokenizer.countTokens();
numberOfLines++;
}
System.out.println("Number of lines :"+numberOfLines);
System.out.println("Number of words :"+numberOfWords);
答案 2 :(得分:0)
使用Regex
String str = "A B C\n D E F\n";
Pattern compile = Pattern.compile("\n");
Matcher matcher = compile.matcher(str);
int count = 0;
while(matcher.find()){
count++;
}
System.out.println(count);//2
count=0;
Pattern compile1 = Pattern.compile("\\s+");
Matcher matcher1 = compile1.matcher(str);
while(matcher1.find()){
count++;
}
System.out.println(count);//6
答案 3 :(得分:0)
你也可以尝试这个
int line=str.trim().split("\n").length;
int words=str.trim().split("\\s+").length;