How to Split String into Only Two Parts with a Given Character in Ruby

How to split string into only two parts with a given character in Ruby?

String#split takes a second argument, the limit.

str.split(' ', 2)

should do the trick.

How to split a string into only two parts, by the last occurrence of the split char?

String#rpartition, e.g.

irb(main):068:0> str = "Angry Birds 2.4.1"
=> "Angry Birds 2.4.1"
irb(main):069:0> str.rpartition(' ')
=> ["Angry Birds", " ", "2.4.1"]

Since the returned value is an array, using .first and .last would allow to treat the result as if it was split in two, e.g

irb(main):073:0> str.rpartition(' ').first
=> "Angry Birds"
irb(main):074:0> str.rpartition(' ').last
=> "2.4.1"

How to split string into 2 parts after certain position

This should do it

[str[0..5], str[6..-1]]

or

 [str.slice(0..5), str.slice(6..-1)]

Really should check out http://corelib.rubyonrails.org/classes/String.html

Split string by multiple delimiters

word = "Now is the,time for'all good people"
word.split(/[\s,']/)
=> ["Now", "is", "the", "time", "for", "all", "good", "people"]

How to split string into 2 parts if it is longer then 90 chars

This is what you need:

string.split(',').in_groups_of(90, false)

Result:

> str.split(',').in_groups_of(10, false)
=> [["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
["11", "12", "13", "14", "15", "16", "17", "18", "19", "20"],
["21", "22", "23", "24", "25", "26", "27", "28", "29", "30"],
["31", "32", "33", "34", "35", "36", "37", "38", "39", "40"],
["41"]]

Or an array with joined values:

str.split(',').in_groups_of(10, false).map {|s| s.join(',')}

Result:

> str.split(',').in_groups_of(10, false).map {|s| s.join(',')}
=> ["1,2,3,4,5,6,7,8,9,10",
"11,12,13,14,15,16,17,18,19,20",
"21,22,23,24,25,26,27,28,29,30",
"31,32,33,34,35,36,37,38,39,40",
"41"]

UPDATE:

With plain Ruby (not Rails):

str.split(',').each_slice(10).to_a

or joined:

str.split(',').each_slice(10).map {|s| s.join(',')}

How to split a string which contains multiple forward slashes

I think, this should help you:

string = './component/unit'

string.split('./')
#=> ["", "component/unit"]

string.split('./').last
#=> "component/unit"

How to split string into only two parts with a given separator in Swift?

You can use split on a characters property of a string and set the maxSplits parameter to 1. For example:

let splitString = "1,Froederick,Frankenstien".characters.split(separator: ",", maxSplits: 1)

The result is an array of CharacterView that need to be converted into strings for example using map along with init(_ characters:) initializer of a String.

let strings = splitString.map { String($0) }

This should produce an array ["1", "Froederick,Frankenstien"].

Ruby #split( ) vs #chars on a string

What is the difference between split and chars [...]?

string.chars parses the underlying bytes to returns the string's characters, whereas string.split('') uses a regular expression to achieve the same.

As a result, chars is faster and more robust. It even works if the string contains invalid characters:

"foo\x80bar".chars
#=> ["f", "o", "o", "\x80", "b", "a", "r"]

Whereas split fails if the string is malformed (because the regex engine can't handle it):

"foo\x80bar".split('')
#=> ArgumentError: invalid byte sequence in UTF-8

If I'm not mistaken, split('') is equivalent to split(//).

Is there a scenario where one is preferable?

split('') can be found in many tutorials. I assume this is because prior to Ruby 2.x, chars returned an enumerator. So in order to get an array you had to use two method calls:

string.chars.to_a

or a single call to: (which is also slightly shorter)

string.split('')

Nowadays, you'd use chars (or each_char for the pre-2.x behavior)

How to split a string in x equal pieces in ruby

How about this?

str.chars.each_slice(2).map(&:join)


Related Topics



Leave a reply



Submit