I have below mentioned string
String str = "\nArticle\n\nArticle\nArticle";
I want total number of count. How can i get this? As the string contain \n so always it gives 1 instead of 3
I have below mentioned string
String str = "\nArticle\n\nArticle\nArticle";
I want total number of count. How can i get this? As the string contain \n so always it gives 1 instead of 3
To get you started, I will show you a simple example:
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);