public class ar {
public static void main(String [] args)
{
String text ="To be or not to be";
int count = 0;
char separator = ' ';
int index = 0;
do
{
++count;
++index; //这里++index会导致index变成1 ,下面index  = text.indexOf(separator, index);为什么要从1开始?字符串String text ="To be or not to be";不应该是从0开始算的吗??0应该对应字符串T,1对应o,难道是从o开始索引的?
index  = text.indexOf(separator, index);
}while(index != -1);

String [] subStr = new String[count];
index = 0;
int endIndex = 0;

for(int i=0;i<count;++i)
{
endIndex = text.indexOf(separator,index);

if(endIndex == -1)
{
subStr[i] = text.substring(index);
}
else
{
subStr[i] = text.substring(index, endIndex);
}

index = endIndex +1;

}
for(String s:subStr)
{
System.out.println(s);
}
}

}

解决方案 »

  1.   

    这里主要是为了需要index在每次循环以后要加一
      

  2.   


    String text ="To be or not to be";
            int count = 0;
            char separator = ' ';
            int index = 0;
            ++count;
            ++index;
            System.out.println(index);//这里index=1
            //下面从T开始技术,T的位置为0,o的位置为1,空格的位置为2,返回2,所以Index=2
            index  = text.indexOf(separator, index);
            System.out.println(index);//这里输出2
      

  3.   

    楼主,楼主上面的那个++index起到的主要作用是自增以退出循环,改成index++也是一样的。如下:do{
    ++count;
    index++; //改成这样也是一样的,主要的作用是让index自增,退出循环
    index  = text.indexOf(separator, index); //其实index输出的是空格所在的索引的位置
    System.out.println("index = " + index);
    }while(index != -1);
    //上面的循环主要是获得count的值
    String [] subStr = new String[count];
    解析这个字符串何必要做两次呢循环呢,其实很简单的,几行代码就可以了:
    public class ar {
    public static void main(String [] args){
    String text ="To be or not to be";
    String[] textArr = text.split(" ");
    for(int i = 0;i < textArr.length;i++ ){
    System.out.println("text[" + i +"] = " + textArr[i]);
    }
    }
    }
      

  4.   

    本想帮忙整理一下,但是查看了你的代码。解释一下:
    首先字符串的第一位索引是0
    可以用此方法测试:charAt(int index)
               返回指定索引处的 char 值。console:T
    关于indexof方法。indexOf(int ch, int fromIndex)
               返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。所以在你的程序里。你第一次的位置是2.打印log代码如下:package io;public class Test {
        public static void main(String [] args)
        {
            String text ="To be or not to be";
            System.out.println(text.length());
            System.out.println(text.charAt(0));
            int count = 0;
            char separator = ' ';
            int index = 0;
            System.out.println("开始index: " + 0);
            do
            {
                ++count;
                ++index; //这里++index会导致index变成1 ,下面index  = text.indexOf(separator, index);为什么要从1开始?字符串String text ="To be or not to be";不应该是从0开始算的吗??0应该对应字符串T,1对应o,难道是从o开始索引的?
                System.out.println("++index后的结果: " + index);
                index  = text.indexOf(separator, index);
                System.out.println("++count后的结果: " + count);
                System.out.println("++index of后的结果: " + index);
            }while(index != -1);
            
            String [] subStr = new String[count];
            index = 0;
            int endIndex = 0;
            
            for(int i=0;i<count;++i)
            {
                endIndex = text.indexOf(separator,index);
                
                if(endIndex == -1)
                {
                    subStr[i] = text.substring(index);
                    System.out.println("索引等于-1 : " + subStr[i]);
                }
                else
                {
                    subStr[i] = text.substring(index, endIndex);
                    System.out.println("索引为2,5,7,9,12,15的时候 : " + subStr[i]);
                }
                
                index = endIndex +1;
                
            }
            for(String s:subStr)
            {
                System.out.println(s);
            }
        }
    }console:
    18
    T
    开始index: 0
    ++index后的结果: 1
    ++count后的结果: 1
    ++index of后的结果: 2
    ++index后的结果: 3
    ++count后的结果: 2
    ++index of后的结果: 5
    ++index后的结果: 6
    ++count后的结果: 3
    ++index of后的结果: 8
    ++index后的结果: 9
    ++count后的结果: 4
    ++index of后的结果: 12
    ++index后的结果: 13
    ++count后的结果: 5
    ++index of后的结果: 15
    ++index后的结果: 16
    ++count后的结果: 6
    ++index of后的结果: -1
    索引为2,5,7,9,12,15的时候 : To
    索引为2,5,7,9,12,15的时候 : be
    索引为2,5,7,9,12,15的时候 : or
    索引为2,5,7,9,12,15的时候 : not
    索引为2,5,7,9,12,15的时候 : to
    索引等于-1 : be
    To
    be
    or
    not
    to
    be
      

  5.   

    我猜LZ这是一个道面试题,要求截取字符串。已空格截取,而且不准使用JDK提供的截取方法吧。
    split方法:package io;public class TestStr {
        public static void main(String []args) {
            String text ="To be or not to be";
            String []a = text.split(" ");
            for(String b: a) {
                System.out.println("b:" + b);
            }
        }
        }