How to Split a String to Get All the Substrings by Ruby

What is the best way to split a string to get all the substrings by Ruby?

def split_word s
(0..s.length).inject([]){|ai,i|
(1..s.length - i).inject(ai){|aj,j|
aj << s[i,j]
}
}.uniq
end

And you can also consider using Set instead of Array for the result.

PS: Here's another idea, based on array product:

def split_word s
indices = (0...s.length).to_a
indices.product(indices).reject{|i,j| i > j}.map{|i,j| s[i..j]}.uniq
end

How to split a string in n substring?

Not sure I completely understand the intention but how about something like this

def string_splitter(str,group_length) 
str.each_char.each_slice(group_length).map(&:join)
end

str = "a9547e"
string_splitter(str,2)
#=> ["a9","54","7e"]
string_splitter(str,3)
#=> ["a94","47e"]

Caveat if the string is not evenly divisible this will offer an uneven ending group e.g.

str = "a9547eP"
string_splitter(str,2)
#=> ["a9","54","7e", "P"]

How to split a string in Ruby and get all items except the first one?

Try this:

first, *rest = ex.split(/, /)

Now first will be the first value, rest will be the rest of the array.

Ruby: Split a string into substring of maximum 40 characters

Your first attempt:

sentence[0..40].gsub(/\s\w+$/,'')

almost works, but it has one fatal flaw. You are splitting on the number of characters before cutting off the last word. This means you have no way of knowing whether the bit being trimmed off was a whole word, or a partial word.

Because of this, your code will always cut off the last word.

I would solve the problem as follows:

sentence[/\A.{0,39}[a-z]\b/mi]
  • \A is an anchor to fix the regex to the start of the string.
  • .{0,39}[a-z] matches on 1 to 40 characters, where the last character must be a letter. This is to prevent the last selected character from being punctuation or space. (Is that desired behaviour? Your question didn't really specify. Feel free to tweak/remove that [a-z] part, e.g. [a-z.] to match a full stop, if desired.)
  • \b is a word boundary look-around. It is a zero-width matcher, on beginning/end of words.
  • /mi modifiers will include case insensitive (i.e. A-Z) and multi-line matches.

One very minor note is that because this regex is matching 1 to 40 characters (rather than zero), it is possible to get a null result. (Although this is seemingly very unlikely, since you'd need a 1-word, 41+ letter string!!) To account for this edge case, call .to_s on the result if needed.


Update: Thank you for the improved edit to your question, providing a concrete example of an input/result. This makes it much clearer what you are asking for, as the original post was somewhat ambiguous.

You could solve this with something like the following:

sentence.scan(/.{0,39}[a-z.!?,;](?:\b|$)/mi)
  • String#scan returns an array of strings that match the pattern - so you can then re-join these strings to reconstruct the original.
  • Again, I have added a few more characters (!?,;) to the list of "final characters in the substring". Feel free to tweak this as desired.
  • (?:\b|$) means "either a word boundary, or the end of the line". This fixes the issue of the result not including the final . in the substrings. Note that I have used a non-capture group (?:) to prevent the result of scan from changing.

How to split a string into consecutive substrings of length at most 3 in all possible ways?

Here's a working function. May be not optimal as I didn't spend much time on it.

str = "1234567890"

def f(s, n)
return [[]] if s.empty?

(1..[n, s.length].min).map{|c| f(s[c..-1], n).map{|a| [s[0, c]] + a}}.inject(&:+)
end

puts f(str, 3).collect{|l| l * "\t"}

EDIT: Made it a bit shorter and the length is now passed as second parameter to function for flexibility.

How to split a string in x equal pieces in ruby

How about this?

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

How to split a sentence into multiple parts in Ruby

str = "one two three four five".split
1.upto(str.size).flat_map { |i| str.each_cons(i).to_a }

#⇒ [["one"], ["two"], ["three"], ["four"], ["five"],
# ["one", "two"], ["two", "three"], ["three", "four"], ["four", "five"],
# ["one", "two", "three"], ["two", "three", "four"], ["three", "four", "five"],
# ["one", "two", "three", "four"], ["two", "three", "four", "five"],
# ["one", "two", "three", "four", "five"]]

Ruby remove all substrings that begin with specific character

I would do something like this:

string = "leo is #confused about #ruby #gsub"
#=> "leo is #confused about #ruby #gsub"
string.split.select { |word| word.start_with?('#') }.join(' ')
#=> "#confused #ruby #gsub"

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 take a random slice of a string in Ruby?

You can write it simpler with:

s[rand(s.length), rand(s.length - 1) + 1]


Related Topics



Leave a reply



Submit