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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值