日期:2014-05-20 浏览次数:20753 次
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);
}
}
}
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
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(" ");