0

I have a string and want to sub-string till 3rd occurrence of "," . I can achieve this using Array. here is the code

String test ="hi,this,is,a,string.";
String[] testArray = test.split(",");
System.out.println(testArray[0]+","+testArray[1]+","+testArray[2]);

Output is :- hi,this,is

Is there anyway to achieve the same using "substring(0, text.indexOf(","))" method.Second thing is there could be some instances where there is no "," in the string and i want to handle both scenarios

Thanks in advance

4 Answers 4

1

I'm not sure if I really recommend this, but — yes; there's a two-arg overload of indexOf that lets you specify the starting position to search at; so you can write:

final int firstCommaIndex = test.indexOf(',');
final int secondCommaIndex = test.indexOf(',', firstCommaIndex + 1);
final int thirdCommaIndex = test.indexOf(',', secondCommaIndex + 1);
System.out.println(test.substring(0, thirdCommaIndex));
Sign up to request clarification or add additional context in comments.

3 Comments

But it will throw the out of the bound exception if "," is not in the String
@user2142786 then you can use try-catch to cover it if you are unsure about occurrence
@user2142786: Well, it has to do something in that case. (Your code, for example, will throw ArrayIndexOutOfBoundsException if there's no comma in the string.) Is there a different behavior that you'd prefer?
0

So what you are looking for is basically a way to receive the n-th (3rd) index of a char (,) in your String. While there is no functionality for that in Java's Standard library, you can either create your own construct (which could look like this answer),

or alternatively you could use StringUtils from Apache, making your desired solution look smth like this:

String test ="hi,this,is,a,string.";
int index = StringUtils.ordinalIndexOf(test, ",", 3);
String desired = test.substring(0, index);
System.out.println(desired);

Comments

0

Other way with stream java 8. This can handle both of your scenarios

System.out.println(Stream.of(test.split(",")).limit(3).collect(Collectors.joining(",")));

Comments

0

You can use regex to achieve this:

import java.util.regex.*;

public class TestRegex {
    public static void main(String []args){
        String test = "hi,this,is,a,string.";

        String regex = "([[^,].]+,?){3}(?=,)";

        Pattern re = Pattern.compile(regex);

        Matcher m = re.matcher(test);
        if (m.find()) {
            System.out.println(m.group(0));
        }
     }
}

2 Comments

Output is :- hi,this,is, . There is an extra "," at the end. is this can be remove with the help of regex or we need to use lastindex of method
@user2142786 Check my updated code. I edited the regex ([[^,].]+,?){3}(?=,).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.