2

I'm looking to split a string at the first ':', to prevent issues if the second part of the string contains a ':'. I've been looking at Regexes, but am still having some issues, can somebody give me a hand? Thanks.

1
  • 3
    Could you include an example of expected input and output? Commented Dec 1, 2012 at 21:29

3 Answers 3

10

You can use the overload of split that takes a limit parameter:

String[] result = s.split(":", 2);
Sign up to request clarification or add additional context in comments.

Comments

7

You can use 2 argument String#split to specify the number of elements you want in your array obtained after split: -

String str = "rohit:jain:use:single:split";
String[] arr = str.split(":", 2);

Comments

1

Notice, that split uses regexps. And indexOf + substr can be much faster than split.

Comments

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.