Reputation: 19
I want to split a String based on first occurrence but when I try to use String.split("\_",1) - It gives me arrayOutOfBounds Exception
array[0] = "this_first";
array[1] = "Not_first";
array[2] = "Maybe_Like_this";
array[3] = "This_is_definitely_it";
for(int i=0;i<array.length;i++){
tmparr = array[i].split("\\_");
firstWord = tmparr[0];
System.out.println(firstWord);
tempString = tmparr[1];
I want just the first word in tmparr[0] and rest all in tmparr[1]. Please advice
Upvotes: 0
Views: 44
Reputation: 14837
The limit
parameter for String#split(String, int)
is defined a little bit strangely. Go back and read the Javadoc, particularly the paragraph starting "The limit parameter...".
TL;DR: What you probably need is this:
tmparr = array[i].split("_", 2);
(Backslash also not required)
Upvotes: 1