Rails beginner
Rails beginner

Reputation: 14504

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.

Upvotes: 10

Views: 11269

Answers (1)

2potatocakes
2potatocakes

Reputation: 2260

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"

Upvotes: 23

Related Questions