前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中输出一个字符串里面的空格,字母还有数字的数目举例说明

java中输出一个字符串里面的空格,字母还有数字的数目举例说明

作者头像
马克java社区
修改2021-03-10 09:37:38
1.2K0
修改2021-03-10 09:37:38
举报
文章被收录于专栏:java大数据

9.6 About string,"I am a teacher",这个字符串中有多少个字,且分别把每个字打印出来。

 

/*本题的思路就是,当我有一个字符串,我需要一个一个字符的处理,当下一个字符是个空格的时候,我就知道前面已 经构成了一个完整的字,把它输出出来就好了。如果发现下一个字符不是一个空格的话,我就把这个字符,加到另一个字符串中,逐渐积累那个字符串成为一个完整 的字。*/

public class Test {

    static int amount_space = 0; //此变量用来记录空格的数量。the variable named amount_space is used to count the number of the space.

    static int flag_Pro = 0; //此变量用来记录现在处理到大字符串中哪一个字符了。this pointer is used to remember the position where we look over.

    static String newstring = "I am a teacher";

    public static void main(String[] args) {

        String outputword = "";

        for (int i = flag_Pro; i < newstring.length(); i++) {

            if (newstring.substring(i, i + 1).equals(" ")) { //假如newstring.substring(i, i + 1)马克-to-win,取出的字符是个空格,就执行这段程序。

                System.out.println(outputword);

                outputword = "";

                amount_space++;

                flag_Pro++; // and next time we will start at a new position

            } else {//newstring.substring(i, i + 1);如果不是一个空格,就加到outputword中。

                outputword = outputword + newstring.substring(i, i + 1);

                flag_Pro++;

            }

        }

        System.out.println(outputword);

        outputword = "";System.out.println("共有"+amount_space+"个空格");

        System.out.println("共有"+(++amount_space)+"个字");

    }

}

 

更多请看:https://blog.csdn.net/qq_44639795/article/details/103143030

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档