split string only on first instance - java

JavaStringSplit

Java Problem Overview


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 https://stackoverflow.com/questions/4607745/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

Java Solutions


Solution 1 - Java

string.split("=", 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" }

Solution 2 - Java

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)

Solution 3 - Java

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));

Solution 4 - Java

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.

Solution 5 - Java

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]);
	}
}

Solution 6 - Java

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 :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondraculaView Question on Stackoverflow
Solution 1 - JavaZaheer AhmedView Answer on Stackoverflow
Solution 2 - JavacodeManView Answer on Stackoverflow
Solution 3 - JavaSivaView Answer on Stackoverflow
Solution 4 - JavaKailash KarkiView Answer on Stackoverflow
Solution 5 - JavaBholaView Answer on Stackoverflow
Solution 6 - JavaMartin KošťálView Answer on Stackoverflow