Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby/Rails method to insert stuff in string after x characters or words

Which method has a function to insert something after x characters.

Example, I have this string:

@test = "Something, is wrong"

And then it would be possible to do something like:

@test.insert(x words or x characters , '<br />')

I need this because I need to insert a line break <br /> in a long string that I need to break up.

like image 523
Rails beginner Avatar asked Feb 21 '13 22:02

Rails beginner


1 Answers

You should start by looking at the String class in the Ruby docs.. http://www.ruby-doc.org/core-1.9.3/String.html

Take a stab at what you think the method you're looking for might be called... in your case insert and see what it says..

"abcd".insert(3, 'X')    #=> "abcXd"
like image 125
2potatocakes Avatar answered Oct 10 '22 14:10

2potatocakes