203

I want to split a string by '=' charecter. But I want it to split on first instance only. How can I do that ? Here is a JavaScript example for '_' char but it doesn't work for me split string only on first instance of specified character

Example :

apple=fruit table price=5

When I try String.split('='); it gives

[apple],[fruit table price],[5]

But I need

[apple],[fruit table price=5]

Thanks

Community
  • 1
  • 1
dracula
  • 4,413
  • 6
  • 26
  • 31

6 Answers6

372
string.split("=", limit=2);

As String.split(java.lang.String regex, int limit) explains:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

The string boo:and:foo, for example, yields the following results with these parameters:

Regex Limit    Result
:     2        { "boo", "and:foo" }
:     5        { "boo", "and", "foo" }
:    -2        { "boo", "and", "foo" }
o     5        { "b", "", ":and:f", "", "" }
o    -2        { "b", "", ":and:f", "", "" }
o     0        { "b", "", ":and:f" }
Blundell
  • 75,855
  • 30
  • 208
  • 233
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • 2
    It's confusing to understand but it just works! Thanks! – Clint Eastwood Feb 25 '16 at 17:04
  • 13
    Some clarification: Limit 2 will return a list of AT MOST 2 elements. It may return a list of 1 element if there is no match to the expression. If there are 2 matches to the expression, the second element of the returned array will not be split. – modle13 Jan 03 '18 at 15:25
18

Yes you can, just pass the integer param to the split method

String stSplit = "apple=fruit table price=5"

stSplit.split("=", 2);

Here is a java doc reference : String#split(java.lang.String, int)

codeMan
  • 5,730
  • 3
  • 27
  • 51
9

As many other answers suggest the limit approach, This can be another way

You can use the indexOf method on String which will returns the first Occurance of the given character, Using that index you can get the desired output

String target = "apple=fruit table price=5" ;
int x= target.indexOf("=");
System.out.println(target.substring(x+1));
Siva
  • 1,938
  • 1
  • 17
  • 36
  • 2
    This doesn't split but extracts the second part. Taking the first part would require yet another line of code...4 lines vs. 1 = thumbs down – Clint Eastwood Feb 25 '16 at 17:06
2
String string = "This is test string on web";
String splitData[] = string.split("\\s", 2);

Result ::
splitData[0] =>  This
splitData[1] =>  is test string  


String string = "This is test string on web";
String splitData[] = string.split("\\s", 3);

Result ::
splitData[0] =>  This
splitData[1] =>  is
splitData[1] =>  test string on web

By default split method create n number's of arrays on the basis of given regex. But if you want to restrict number of arrays to create after a split than pass second argument as an integer argument.

Kailash Karki
  • 2,106
  • 1
  • 12
  • 6
0

This works:

public class Split
{
    public static void main(String...args)
    {
        String a = "%abcdef&Ghijk%xyz";
        String b[] = a.split("%", 2);
        
        System.out.println("Value = "+b[1]);
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Bhola
  • 392
  • 1
  • 15
-2
String[] func(String apple){
String[] tmp = new String[2];
for(int i=0;i<apple.length;i++){
   if(apple.charAt(i)=='='){
      tmp[0]=apple.substring(0,i);
      tmp[1]=apple.substring(i+1,apple.length);
      break;
   }
}
return tmp;
}
//returns string_ARRAY_!

i like writing own methods :)

  • 3
    This method is not too useful, as the separator is hardcoded. Furthermore, the implementation of linear search with a for-loop brings no advantage over using String.indexOf to find the location of the first separator. Third, it is not intuitive to see how many String elements are returned. As we talk about splitting things into two, consider using the Pair data type which represents exactly two strings instead of creating an array. – Gee Bee Sep 29 '16 at 01:13