Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

Count words in my string with special characters java

In total there are 51 words in the string however my code returns to me 56 words which I don't understand why.

public class PartB

{ // instance variables - replace the example below with your own

public static int countWords(String str) 
{ 
      
    // Check if the string is null 
    // or empty then return zero 
    if (str == null || str.isEmpty()) 
        return 0; 
      
    // Splitting the string around 
    // matches of the given regular 
    // expression 
    String[] words = str.split("[\s+,'/]"); 
      
    // Return number of words 
    // in the given string 
    return words.length; 
} 

public static void main(String args[]) 
{ 
      
    // Given String str 
    String str = "Sing, sing a song/Let the world sing along/" +
    "Sing of love there could be/Sing for you and for me/" +
    "Sing, sing a song/Make it simple to last/" +
    "Your whole life long/Don't worry that it's not/" +
    "Good enough for anyone/Else to hear/" +
    "Just sing, sing a song";
     
      
    // Print the result 
    System.out.println("No of words : " + 
       countWords(str)); 
} 

}

question from:https://stackoverflow.com/questions/66067785/count-words-in-my-string-with-special-characters-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two errors in your [s+,'/] regex:

  • The + plus should be outside the [ ] character class.

    Reason: Without the +, text "Sing, sing" would have 2 separators, a comma and a space, with an empty token between them, and you're counting that empty token.

  • The ' apostrophe should not be there.

    Reason: With the ', text Don't would be 2 words, not 1.

So the regex should be: [s,/]+

Changing nothing but the split call to split("[\s,/]+"), the result becomes:

No of words : 51

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...