I have:
1234 2345 3456 4567
When I try String.split(" ",2)
, I get:
{1234} , {2345 3456 4567}
But I need:
{1234},{2345}
I want only the first two elements. How do I implement this in Java?
Thanks in advance.
EDIT:This is just one line of a huge dataset.
To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.
i.e. if a string contains the letter c at indices 1, 5 and 10 the results are as follows: =FIND("c",A1,1) will find the first c =FIND("c",A1,2) through =FIND("c",A1,5) will find the second c =FIND("c",A1,6) through =FIND("c",A1,10) will find the third c.
split() The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings.
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
I assume you need first two string, then do the following:
String[] res = Arrays.copyOfRange(string.split(" "), 0, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With