前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中如何知道一个字符串中有多少个字,把每个字打印出来,举例

java中如何知道一个字符串中有多少个字,把每个字打印出来,举例

作者头像
马克java社区
修改2021-03-11 09:48:29
5900
修改2021-03-11 09:48:29
举报
文章被收录于专栏:java大数据

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

public class Test {

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

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

    staticString newstring = "I am a teacher!";

    publicstatic 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,取出的字符是个空格,就执行这段程序。

             if(!outputword.equals("")){System.out.println(outputword);outputword = "";}

             amount_space++;

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

               continue;

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

更多请见:https://blog.csdn.net/qq_44639795/article/details/103142891

本文系转载,前往查看

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

本文系转载前往查看

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

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