3

I have to insert a string after a certain character using Ruby.

For example, if I have a line like the following:

(N D CGYRWIFGD2S7 0 1 N)(N D CGYCGYOVFBK0 0 N N)(ISA N N N CGYCG3FEXOIS N PUB NONE N N 0)(ISA N N N CGYCGYFGAOIS N PUB NONE N N 0)(ISA N N N CGYCG2FGAOIS N PUB NONE N N 0)(N D CGYCGYOVFBK1 0 N N)(N D CGYLOCFGA2S7 0 N N)(N D CGY01TFGD2S7 0 N N)(N D CGY01TCASUAL 0 N N)(N D CGYATTUSAOS7 0 1 N)(ISA N N N CGYAGTAD4OIS N PUB NONE 0 N 7)

I'd like to insert the html tag <br /> after every closing bracket ")".

I guess I can use regex, but every line has different number of brackets. So this particular line can have 5, while other line can have 20. With my limited knowledge of Ruby or in programming in general, I am seeking for help :)

Thanks!

3 Answers 3

16
s.gsub(')', ')<br />')
Sign up to request clarification or add additional context in comments.

Comments

8

Use gsub to globally replace.

my_string.gsub(/\)/, ")<br />");

Comments

0

Or, use split and join:

delim = ')'
s.split(delim).join(delim + '<br />')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.