I'm trying to take one whole string, and print each section of it differently using index of. I've been trying something like this...

String example = "one, two, three, four"
int comma = example.indexOf(',' , 0);
    String output = example.substring(comma);
    System.out.println(output);

This prints

,two,three,four

I can't get it to do anything else...

有帮助吗?

解决方案

Only using indexOf method with loop you can print all separate String separated by comma ,. You don't need split regex. Look at following example.

    String str = "one, two, three, four";
    int lastIndex = 0;
    int firstIndex=0;
    while (lastIndex != -1) {

        lastIndex = str.indexOf(',', lastIndex);

        if (lastIndex != -1) {
            System.out.print(str.substring(firstIndex, lastIndex));

            if(lastIndex==str.lastIndexOf(',')){
            System.out.print(str.substring(lastIndex));
            }
            lastIndex += 1;
        }

        firstIndex=lastIndex;
    }
    System.out.println();

Output: one two three four

其他提示

try this:

String example = "one, two, three, four";

for (int i = example.indexOf(", "); i != -1; i = example.indexOf(", ")) {
    System.out.println(example.substring(0, i));
    example = example.substring(i + 2);
}
System.out.println(example);

if you like recursion:

public static void main(String[] args) {
    String example = "one, two, three, four";
    printSections(example);
}

public static void printSections(String word) {
    int i = word.indexOf(", ");
    if (i == -1) System.out.println(word);
    else {
        System.out.println(word.substring(0, i));
        printSections(word.substring(i + 2));
    }
}

Why dont you try with split()?

String example = "one, two, three, four"
String[] temp = example.split(",");
for(int i=0;i<temp.length; i++){
System.out.println(temp[i]);
}

There is a handy method in String class. Sting#split.

You just split your string with comma, that is all what you need.

String example = "one, two, three, four";
String[] split = example.split(",");
for (String string : split) {
    System.out.println(string);
}

split method returns the array of separated and string's and we just need to print them.

Side note: I used Enhanced for loop to iterate

do like this

String example = "one, two, three, four"
String[] output = example.split(',');
for(String s:output){  
    System.out.println(s);
}

Use Split

String example = "one, two, three, four";
    for(String i:example.split(",")){
        System.out.println(i);

    }

Using IndexOf

    String example = "one, two, three, four";
    String output;
    String temp = example;
    int gIndex = 0;
    int len = example.length();
    example += ",";
    for(int i=0 ; i<len;i+=gIndex+1){
        gIndex = example.indexOf(",");
        output = example.substring(0,gIndex);
        System.out.println(output);
        example = example.replace(output, "").replaceFirst(",", "");
    }
    example = temp;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top