[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

add text at beginning of a string

Fernando Perez

12/20/2008 5:13:00 PM

Hi,

Sorry for this silly question, but I don't remember how to insert text
to the beginning of a string. It's like the concatenate (or <<) operator
but it adds text before.


Thanks
--
Posted via http://www.ruby-....

5 Answers

Jesús Gabriel y Galán

12/20/2008 5:25:00 PM

0

On Sat, Dec 20, 2008 at 6:13 PM, Fernando Perez <pedrolito@lavache.com> wrote:
> Hi,
>
> Sorry for this silly question, but I don't remember how to insert text
> to the beginning of a string. It's like the concatenate (or <<) operator
> but it adds text before.

I recommend studying this page, it's good to be familiar with the
methods in class String:

http://ruby-doc.org/core/classes/S...

The one you are looking for is:

irb(main):001:0> s = "world"
=> "world"
irb(main):002:0> s.insert(0, "hello ")
=> "hello world"
irb(main):003:0> s
=> "hello world"

Hope this helps,

Jesus.

Joel VanderWerf

12/20/2008 8:31:00 PM

0

Phlip wrote:
>> irb(main):002:0> s.insert(0, "hello ")
>
> ['hello ', s].join
>
> ...and how about unshift?
>
> Any sicker ways out there?? Besides s = 'hello ' + s?

irb(main):001:0> s = " world"
=> " world"
irb(main):002:0> s[/\A/] = "hello,"
=> "hello,"
irb(main):003:0> s
=> "hello, world"


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Phlip

12/20/2008 8:31:00 PM

0

> irb(main):002:0> s.insert(0, "hello ")

['hello ', s].join

....and how about unshift?

Any sicker ways out there?? Besides s = 'hello ' + s?

--
Phlip

Brian Candler

12/21/2008 10:00:00 PM

0

>> Any sicker ways out there?? Besides s = 'hello ' + s?

irb(main):001:0> s = " world"
=> " world"
irb(main):002:0> s[0,0] = "hello"
=> "hello"
irb(main):003:0> s
=> "hello world"
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

12/22/2008 8:00:00 AM

0

On Sat, Dec 20, 2008 at 9:26 PM, Phlip <phlip2005@gmail.com> wrote:
>> irb(main):002:0> s.insert(0, "hello ")
>
> ['hello ', s].join
>
> ...and how about unshift?
>
> Any sicker ways out there?? Besides s = 'hello ' + s?

Just a note: both your join method and the + method above will create
a new string object, while the other methods in this thread (insert,
[/\A/], [0,0]) will modify the string in place. This might or might
not matter to the OP.

Regards,

Jesus.