String is ex="test1, test2, test3, test4, test5"
when I use
ex.split(",").first
it returns
"test1"
Now I want to get the remaining items, i.e. `"test2, test3, test4, test5". If I use
ex.split(",").last
it returns only
"test5"
How to get all the remaining items skipping first one?
Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.
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.
split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified. Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches.
Try this:
first, *rest = ex.split(/, /)
Now first
will be the first value, rest
will be the rest of the array.
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