Answer the question
In order to leave comments, you need to log in
How to split a string in ruby on the first occurrence or on a specific word?
Let's say there is a string "one word and many words in a sentence"
.
If I do , then the result will be .
And you need to get :.
That is, split by the first occurrence found in the string. Or split the string exactly by the word word, and if there are more letters in the word, then this is not considered a match. str.split('word')
['one ', ' and many ','s in a sentence']
['one ', ' and many words in a sentence']
Answer the question
In order to leave comments, you need to log in
Using the regular expression \bword\b
, where \b
means a word boundary:
str.split(/\bword\b/)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question