简体   繁体   中英

Extract Comma separated Values from String

I'm trying to print comma separated value from below string

A,B,C,D,E

Here is my Code

String Inp = "A,B,C,D,E";
StringBuilder op = new StringBuilder();
int Idx = 0;
int IdxUpto = 0;
for(;;){
  IdxUpto = Inp.indexOf(",", Idx);
  if(IdxUpto==-1){
    System.out.println("There is no more commas in String");
    op.append(Inp.substring(Idx, Inp.length()));
    System.out.println(op);
    break;
  }
  op.append(Inp.substring(Idx, IdxUpto));
  Idx = IdxUpto;
  System.out.println(op);
}   

For loop is not getting stop after first extraction, Can anyone please help what am doing WRONG ?

Edit:

Expected Output

A
B
C
D
E

Thanks

Here is the offending line in your code:

Idx = IdxUpto;

You should be incrementing plus one, to take into account that you have to also advance past the comma delimiter, which has a width of one. So use this:

Idx = IdxUpto + 1;

But note that you have several other problems. For one thing, you should be following Java conventions and avoid starting your variable names with capital letters. Also, it probably makes more sense to use a while loop here rather than a for loop with no criteria specified. Taking all this into account, here is a working version of your script:

String input = "A,B,C,D,E";
StringBuilder op = new StringBuilder();
int idx = 0;
int idxUpto = 0;
do {
    idxUpto = input.indexOf(",", idx);
    if (idxUpto == -1) {
        String term = input.substring(idx, input.length());
        op.append(term);
        System.out.println(term);
        System.out.println("There is no more commas in String");
        break;
    }
    String term = input.substring(idx, idxUpto);
    op.append(term);
    idx = idxUpto+1;
    System.out.println(term);
} while(true);

A
B
C
D
E
There is no more commas in String

String's indexof api says as below:

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

When you enter your for condition, you get index as 1 (index place where you find first comma). Now what you do is set Idx to this index as below:

Idx = IdxUpto;

Now since index of api starts the search for comma from that index, it keeps on saying i found at index 1 and you keep on trying to search for comma at index 1. To fix the problem, try to add one like below:

Idx = IdxUpto + 1;

Couple of things to note:

a. Use camel case variable naming conventions.
b.You are appending to StringBuilder, if you need desired output as expected, then your println statement should change as:

System.out.println(Inp.substring(Idx, IdxUpto));//mutate Idx assignment after this print statement.

You can simply use String.split() method like this:

public class StringSplit {
  public static void main(String[] args) {
    String input = "A,B,C,D,E";
    String[] array = input.split(",");
    for (String letter : array) {
      System.out.println(letter);
    }
  }
}

Output:

A
B
C
D
E

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM