0
点赞
收藏
分享

微信扫一扫

如何使用Java实现找字符串中不重复的单词个数

1kesou 2023-09-07 阅读 51


import java.util.HashSet;
public class Test{
public static void main(String[] args){
		String str="How are you! I want tell you!";
		System.out.println(countDifferentWords(str));
	}
public static int countDifferentWords(String str){
		String[] words=str.split("\\s+");
		HashSet<String> hs=new HashSet<>();
			for(String word:words){
			hs.add(word);
			}
		return hs.size();
		}
}



举报

相关推荐

0 条评论