I have a string:
"hello\t World\nbla"
I would like to split it to:
["hello\t ",
"World\n",
"bla"]
How would I do this in Ruby?
You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.
Yes, . split() always preserves the order of the characters in the string.
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.
>> "hello\t World\nbla".scan /\S+\s*/
=> ["hello\t ", "World\n", "bla"]
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