how to convert the string?

P

Pen Ttt

i want to convert a string "-34\n \302\240" into "-34"
irb(main):001:0> "-34\n \302\240".gsub('\302\240','').strip.chomp
=> "-34\n \302\240"
how to convert it?
thinks for your help.
 
J

Jörg W Mittag

Pen said:
i want to convert a string "-34\n \302\240" into "-34"
irb(main):001:0> "-34\n \302\240".gsub('\302\240','').strip.chomp
=> "-34\n \302\240"
how to convert it?
thinks for your help.

Does the string have *any* kind of random data at the beginning or is
it always an integer? In the latter case, you could simply parse the
number and convert it back into a string:

"-34\n \302\240".to_i.to_s # "-34"

jwm
 
R

Robert Klemme

i want to convert a string "-34\n \302\240" into "-34"
irb(main):001:0> "-34\n \302\240".gsub('\302\240','').strip.chomp
=> "-34\n \302\240"
how to convert it?

As others have indicate it is a bit unclear what strings you expect. If
you want the first integer you can do

irb(main):001:0> s = "-34\n \302\240"
=> "-34\n "
irb(main):002:0> s.to_i
=> -34
irb(main):003:0> s[/^[-+]?\d+/]
=> "-34"
irb(main):004:0>

Kind regards

robert
 
P

Pen Ttt

irb(main):001:0> "-34\n \302\240".match(/\S+/).to_s
=> "-34"
irb(main):002:0> "-\n \302\240".match(/\S+/).to_s
=> "-"
irb(main):003:0> "-?\n \302\240".match(/\S+/).to_s
=> "-?"
irb(main):004:0> "-jkl?\n \302\240".match(/\S+/).to_s
=> "-jkl?"
irb(main):005:0>

match(/\S+/).to_s is what i want.
 
P

Pen Ttt

if i want to convert
"-34\n \302\240" into "-34\n "
"-34jkl\n \302\240" into "-34jkl\n "
"-34jkl\n tyy \302\240" into "-34jkl\n tyy "
how can i do?
 
P

Pen Ttt

maybe match(/\S+/).to_s is not ok
would you mind to help me to find a way(one way not three way for the
following) to convert

"Dec 31, 2009/n \302\240" into "Dec 31, 2009"
"-34/n \302\240" into "-34"
"profit/n \302\240" into "profit"
 
R

Robert Klemme

maybe match(/\S+/).to_s is not ok
would you mind to help me to find a way(one way not three way for the
following) to convert

"Dec 31, 2009/n \302\240" into "Dec 31, 2009"
"-34/n \302\240" into "-34"
"profit/n \302\240" into "profit"

What output do you expect for every input?

Kind regards

robert
 
P

Pen Ttt

i have solved it myself,thinks.
irb(main):026:0> "-34\n \302\240".gsub("\302\240","")
=> "-34\n "
irb(main):027:0>
irb(main):028:0* "-34jkl\n \302\240".gsub("\302\240","")
=> "-34jkl\n "
irb(main):029:0>
irb(main):030:0* "-34jkl\n tyy \302\240".gsub("\302\240","")
=> "-34jkl\n tyy "
irb(main):031:0>
irb(main):032:0* "Dec 31, 2009/n
\302\240".gsub("\302\240","").split("/n")[0]
=> "Dec 31, 2009"
irb(main):033:0>
irb(main):034:0* "-34/n \302\240".gsub("\302\240","").split("/n")[0]
=> "-34"
irb(main):035:0>
irb(main):036:0* "profit/n \302\240".gsub("\302\240","").split("/n")[0]
=> "profit"
 
H

Harry Kakueki

i want to convert a string =A0"-34\n =A0 \302\240" =A0into "-34"
irb(main):001:0> "-34\n =A0 \302\240".gsub('\302\240','').strip.chomp
=3D> "-34\n =A0 \302\240"
how to convert it?
thinks for your help.
--

I don't think you are clearly stating your requirements.
Also, I think you may be confusing yourself with your first attempt.

Try changing your gsub('\302\240','') to gsub("\302\240","")
(double quotes)

You were sometimes using double quotes and sometimes single quotes.
See if that helps you any.


Harry
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top