用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数?
展开全部
public class StringTest {
public static void main(String[] args) {
//定义一个测试字符串
String testStr = "this is a test string";
//将该字符串按空格分解成单词数组
String[] words = testStr.split(" ");
//输出单词个数
System.out.println("该字符串一共有" + words.length + "个单词!");
//输出每一个单词
for (int i = 0; i < words.length; i++) {
System.out.println("该字符串的第" + (i+1) + "个单词是:" + words[i]);
}
}
}
public static void main(String[] args) {
//定义一个测试字符串
String testStr = "this is a test string";
//将该字符串按空格分解成单词数组
String[] words = testStr.split(" ");
//输出单词个数
System.out.println("该字符串一共有" + words.length + "个单词!");
//输出每一个单词
for (int i = 0; i < words.length; i++) {
System.out.println("该字符串的第" + (i+1) + "个单词是:" + words[i]);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询