How to convert the string?

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.

“-34\n \302\240”.match(/\S+/).to_s

Eugen.

where \S in regular expression means match any character except
whitespace

Pen T. wrote:

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

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.

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?

On 07/23/2010 03:45 PM, Pen T. wrote:

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

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”

On 24.07.2010 05:28, Pen T. wrote:

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

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”

On Fri, Jul 23, 2010 at 10:45 PM, Pen T. [email protected] wrote:

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs