Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
编写一个名为 wordCount 的方法,该方法接受一个字符串作为其参数并返回字符串中的单词数。单词是一个或多个非空格字符的序列(' ' 以外的任何字符)。例如,调用 wordCount("hello") 应该返回 1,调用 wordCount("你好吗?") 应该返回 3,调用 wordCount("this string has wide spaces") 应该返回 5,调用 wordCount (" ") 应该返回 0
input = input.trim() return input.empty()?0:input.split("\\s+").length
String trimmedInput = input.trim(); int wordsInSentence = trimmedInput .isEmpty() ? 0 : trimmedInput.split("\\s+").length;