[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: use reqex to get the middle character and repeat it

Dan Diebolt

3/13/2008 10:00:00 PM

[Note: parts of this message were removed to make it a legal post.]

Don't use a regexp. It isn't clear what you want to happen if there isn't a middle character if the string has a odd integer for its length. Try using some type of substring approach such as this:

str = "xxx"
str[0..str.length/2-1] +
str[str.length/2,1] * 4 +
str[str.length/2+1..str.length]
=> "xxxxxx"


5 Answers

David A. Black

3/13/2008 10:24:00 PM

0

Hi --

On Fri, 14 Mar 2008, Dan Diebolt wrote:

> Don't use a regexp. It isn't clear what you want to happen if there isn't a middle character if the string has a odd integer for its length. Try using some type of substring approach such as this:
>
> str = "xxx"
> str[0..str.length/2-1] +
> str[str.length/2,1] * 4 +
> str[str.length/2+1..str.length]
> => "xxxxxx"

A slightly different way to do (I think) the same thing:

irb(main):036:0> str = "xxx"
=> "xxx"
irb(main):037:0> str[str.size/2] = str[str.size/2].chr * 4
=> "xxxx"
irb(main):038:0> str
=> "xxxxxx"

I was hoping for a *= operation but it doesn't quite fit :-)


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

David A. Black

3/13/2008 10:30:00 PM

0

On Fri, 14 Mar 2008, David A. Black wrote:

> Hi --
>
> On Fri, 14 Mar 2008, Dan Diebolt wrote:
>
>> Don't use a regexp. It isn't clear what you want to happen if there isn't a
>> middle character if the string has a odd integer for its length. Try using
>> some type of substring approach such as this:
>>
>> str = "xxx"
>> str[0..str.length/2-1] +
>> str[str.length/2,1] * 4 +
>> str[str.length/2+1..str.length]
>> => "xxxxxx"
>
> A slightly different way to do (I think) the same thing:
>
> irb(main):036:0> str = "xxx"
> => "xxx"
> irb(main):037:0> str[str.size/2] = str[str.size/2].chr * 4
> => "xxxx"
> irb(main):038:0> str
> => "xxxxxx"
>
> I was hoping for a *= operation but it doesn't quite fit :-)

Actually it does work in 1.9:

irb(main):004:0> str = "xxx"
=> "xxx"
irb(main):005:0> str[str.size/2] *= 4
=> "xxxx"
irb(main):006:0> str

:-)


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Adam Akhtar

3/14/2008 12:35:00 AM

0

ahh why didnt i just think of that... str.gsub(/(\w)(x)(\w)/,
'\1'+'\2'*4+'\3')

In this situation the string will always be 3 chars long and will either
consist of spaces or x so the above will be fine (with a bit of
tweaking).

Thanks very much!

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

Peña, Botp

3/14/2008 2:15:00 AM

0

IyBJbiB0aGlzIHNpdHVhdGlvbiB0aGUgc3RyaW5nIHdpbGwgYWx3YXlzIGJlIDMgY2hhcnMgbG9u
ZyBhbmQgDQoNCnRoaXMgaXMgbm90IHJlZ2V4IDopDQoNCmlyYihtYWluKTowMDc6MD4gUlVCWV9W
RVJTSU9ODQo9PiAiMS45LjAiDQppcmIobWFpbik6MDA4OjA+IHN0cj0ncXhxJw0KPT4gInF4cSIN
CmlyYihtYWluKTowMDk6MD4gc3RyWzBdK3N0clsxXSo0K3N0clsyXQ0KPT4gInF4eHh4cSINCg0K
b3IgbW9yZSBzdWNjaW5jdCwNCg0KaXJiKG1haW4pOjAxMDowPiBzdHJbMV0qPTQNCj0+ICJ4eHh4
Ig0KaXJiKG1haW4pOjAxMTowPiBzdHINCj0+ICJxeHh4eHEiDQoNCg0Ka2luZCByZWdhcmRzIC1i
b3RwDQo=

Sebastian Hungerecker

3/14/2008 8:48:00 AM

0

David A. Black wrote:
> irb(main):036:0> str = "xxx"
> => "xxx"
> irb(main):037:0> str[str.size/2] = str[str.size/2].chr * 4
> => "xxxx"
> irb(main):038:0> str
> => "xxxxxx"
>
> I was hoping for a *= operation but it doesn't quite fit :-)

str[str.size/2,1] *= 4


--
Jabber: sepp2k@jabber.org
ICQ: 205544826