Ruby/Rails方法,在字符串中插入x字符或单词后的字符串。

时间:2021-02-22 01:59:36

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

该方法有一个函数,可以在x字符后插入一些东西。

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.

我需要这个,因为我需要在一个需要拆分的长字符串中插入换行符

1 个解决方案

#1


7  

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

您应该从Ruby文档中的String类开始。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"

#1


7  

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

您应该从Ruby文档中的String类开始。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"