1

For example I have this string params: Blabla,1,Yooooooo,Stackoverflow,foo,chinese

And I want to get the string testCaseParams until the 3rd comma: Blabla,1,Yooooooo

and then remove it and the comma from the original string so I get thisStackoverflow,foo,chinese

I'm trying this code but testCaseParams only shows the first two values (gets index of the 2nd comma, not 3rd...)

 //Get how many parameters this test case has and group the parameters
        int amountOfInputs = 3;
        int index = params.indexOf(',', params.indexOf(',') + amountOfInputs);
        String testCaseParams = params.substring(0,index);
        params = params.replace(testCaseParams + ",","");

5 Answers5

1

One option would be a clever use of String#split:

String input = "Blabla,1,Yooooooo,Stackoverflow,foo,chinese";
String[] parts = input.split("(?=,)");
String output = parts[0] + parts[1] + parts[2];
System.out.println(output);

Demo

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can hold the index of the currently-found comma in a variable and iterate until the third comma is found:

int index = 0;
for (int i=0; i<3; i++) index = str.indexOf(',', index);
String left = str.substring(0, index);
String right = str.substring(index+1);  // skip comma

Edit: to validate the string, simply check if index == -1. If so, there are not 3 commas in the string.

clabe45
  • 2,354
  • 16
  • 27
  • The last param string doesnt have a comma in the end. –  Jan 12 '18 at 11:59
  • @fsdf Do you mean `left` _shouldn't_ have a comma in the end? – clabe45 Jan 12 '18 at 12:02
  • HTTP Status 500 - Request processing failed; nested exception is java.lang.StringIndexOutOfBoundsException: String index out of range: -1 –  Jan 12 '18 at 12:03
  • @fsdf That means that your string does not have three occurrences of `,`. You can add input validation before this code. – clabe45 Jan 12 '18 at 12:04
  • DUDE It'S THE LAST GROUP IT DOESNT HAVE THE COMMA AND IT'S SUPPOSED TO BE LIKE THAT!!! –  Jan 12 '18 at 12:09
0

One can use split with a limit of 4.

String input = "Blabla,1,Yooooooo,Stackoverflow,foo,chinese";
String[] parts = input.split(",", 4);
if (parts.length == 4) {
    String first = parts[0] + "," + parts[1] + "," + parts[2];
    String second = parts[3]; // "Stackoverflow,foo,chinese"
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You can split with this regex to get the 2 pats:

String[] parts = input.split("(?<=\\G.*,.*,.*),");

It will result in parts equal to:

{ "Blabla,1,Yooooooo", "Stackoverflow,foo,chinese" }

\\G refers to the previous match or the start of the string.
(?<=) is positive look-behind.

So it means match a comma for splitting, if it is preceded by 2 other commas since the previous match or the start of the string.

This will keep empty strings between commas.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
0

I offer this here just as a "fun" one line solution:

public static int nthIndexOf(String str, String c, int n) {
    return str.length() - str.replace(c, "").length() < n ? -1 : n == 1 ? str.indexOf(c) : c.length() + str.indexOf(c) + nthIndexOf(str.substring(str.indexOf(c) + c.length()), c, n - 1);
}

//Usage
System.out.println(nthIndexOf("Blabla,1,Yooooooo,Stackoverflow,foo,chinese", ",", 3)); //17

(It's recursive of course, so will blow up on large strings, it's relatively slow, and certainly isn't a sensible way to do this in production.)

As a more sensbile one liner using a library, you can use Apache commons ordinalIndexOf(), which achieves the same thing in a more sensible way!

Michael Berry
  • 70,193
  • 21
  • 157
  • 216