문자열 내용의 단어 및 줄 수를 계산하고 싶습니다.문자열의 줄 및 단어 수를 계산하지 않습니다.
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};
}
이 코드는 잘 작동하지만 exceution 동안 너무 많은 문자열을 생성합니다 : 여기 내 코드입니다. 그래서 똑같은 일을하는 다른 효과적인 방법이 있습니다. 감사.
왜 그냥 문자열/파일의 모든 문자를 반복하지 않는 시도 할 수 있습니다? 공백, 탭 및 줄 바꿈 만 계산하면됩니다. – DaoWen