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中。
。。。。。。。。。。。。。。。。。。
更多请见:http://www.mark-to-win.com/tutorial/java_1_9.6.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值