黑马程序员技术交流社区

标题: 小程序 [打印本页]

作者: 413520953    时间: 2015-11-9 23:42
标题: 小程序
在控制台输入He took a glance over the notes.(字符串我随便找的,格式是这样)
要求是输出字符串中每个单词的长度,
单词与单词之间的空格可能存在多个,
.不能算在最后一个单词当中,
输出的结果是:2 4 1 6 4 3 5
最后不能有空格
class Test {
        public static void main(String[] args) {
                String line = "He  took a glance over the notes.";
                line = line.trim();
                line = line.replace("\\.", "");
                String[] ss = line.split("[ ]+");
                for (int i = 0; i < ss.length; ++i) {
                        System.out.print(ss[i].length() + " ");
                }
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2