您的位置:首页 > 编程语言 > Java开发

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

2014-08-05 14:08 423 查看
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/JavaBeginner/JavaBeginner1_web.html#9.6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐