我有下面提到的字符串
String str = "\nArticle\n\nArticle\nArticle";
我想要总数。我怎么能得到这个? 由于字符串包含\ n所以总是它给出1而不是3
答案 0 :(得分:0)
为了帮助您入门,我将向您展示一个简单的示例:
String str = "\nArticle\n\nArticle\nArticle";
// Split the String by \n
String[] words = str.split("\n");
// Keep the count of words
int wordCount = 0;
for(String word : words){
// Only count non-empty Strings
if(!word.isEmpty()) {
wordCount++;
}
}
// Check, answer is 3
System.out.println(wordCount);