spaces in strings

J

Joonas Lindholm

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.
 
7

7stud --

Joonas said:
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.
 
R

Robert Klemme

2009/8/26 Glenn Jackman said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top