Split a character string based on change of character: Difference between revisions

Added Ruby
(Added Ruby)
Line 382:
input string: gHHH5YY++///\
output string: g, HHH, 5, YY, ++, ///, \
</pre>
 
=={{header|Ruby}}==
<lang ruby>def split(str)
puts " input string: #{str}"
s = str.chars.chunk(&:itself).map{|_,a| a.join}.join(", ")
puts "output string: #{s}"
s
end
 
split("gHHH5YY++///\\")</lang>
 
{{out}}
<pre>
input string: gHHH5YY++///\
output string: g, HHH, 5, YY, ++, ///, \
</pre>
 
Anonymous user