Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > spaces in strings

Reply
Thread Tools

spaces in strings

 
 
Joonas Lindholm
Guest
Posts: n/a
 
      08-26-2009
I have a string like '1 334.64' and I need to remove the space so that
it becomes '1334.64'. I have I think ruby 1.6 in home computer where
worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
doesn't seem to work.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      08-26-2009
Joonas Lindholm wrote:
> Brian Candler wrote:
>
>> ruby 1.7 was only a development version, so you should move to 1.8.6 if
>> possible.
>>
>> However, "it doesn't seem to work" is not a useful problem description.
>> You need to show exactly what you ran, the exact output (copy-paste),
>> what you expected the output to be, and what platform you are running
>> under.
>>
>> The code snippet you have shown looks OK to me.

>
> Ups, what I meant is that the 1.8.6 is the version I have home and at
> other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
> more testing..


I get identical results with 1.8.6 and ruby ruby 1.9.1L

str = "1 334.64"
puts str

str.gsub!(/\s/, "")
puts str

--output:--
1 334.64
1334.64

I have a hard time believing that ruby 1.8.7 would produce different
results--no matter how badly it is screwed up.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ralf Mueller
Guest
Posts: n/a
 
      08-26-2009
Joonas Lindholm wrote:
> Brian Candler wrote:
>
>
>> ruby 1.7 was only a development version, so you should move to 1.8.6 if
>> possible.
>>
>> However, "it doesn't seem to work" is not a useful problem description.
>> You need to show exactly what you ran, the exact output (copy-paste),
>> what you expected the output to be, and what platform you are running
>> under.
>>
>> The code snippet you have shown looks OK to me.
>>

>
> Ups, what I meant is that the 1.8.6 is the version I have home and at
> other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
> more testing..
>

String#tr works similar with easier interface:
http://www.ruby-doc.org/core/classes...g.html#M000830

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-27-2009
2009/8/26 Glenn Jackman <(E-Mail Removed)>:
> At 2009-08-26 12:37PM, "Joonas Lindholm" wrote:
>> =A0I have a string like '1 334.64' and =A0I need to remove the space so =

that
>> =A0it becomes '1334.64'. I have I think ruby 1.6 in home computer where
>> =A0worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
>> =A0doesn't seem to work.

>
> An option nobody has suggested so far: =A0String#delete
>
> =A0 =A0"1 334.64".delete(" ") =A0# =3D=3D> "1334.64"


Another option that I haven't seen suggested:

str.gsub!(/\s+/, '')

Could be a bit faster than /\s/ if there are longer sequences of spaces.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments