Prev: unsubscribe
Next: wxRuby listcontrol
From: Pen Ttt on 23 Jul 2010 09:45 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. -- Posted via http://www.ruby-forum.com/.
From: Eugen Ciur on 23 Jul 2010 10:03 "-34\n \302\240".match(/\S+/).to_s Eugen. On 07/23/2010 04:45 PM, Pen Ttt 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. >
From: Eugen Ciur on 23 Jul 2010 10:09 where \S in regular expression means match any character except whitespace On 07/23/2010 05:05 PM, Eugen Ciur wrote: > "-34\n \302\240".match(/\S+/).to_s > > > > Eugen. > > On 07/23/2010 04:45 PM, Pen Ttt 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. >> >>
From: Jörg W Mittag on 23 Jul 2010 11:21 Pen Ttt 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
From: Robert Klemme on 23 Jul 2010 12:56
On 07/23/2010 03:45 PM, Pen Ttt 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ |