From: David Alan Black Date: 2002-07-23T08:45:15+09:00 Subject: Re: Strings and member-operators Hi -- On Tue, 23 Jul 2002, the Bare wrote: > Hi: > > I'm looking for a way to strip the quotes (and whitespace) off of a > string. Right now, I'm using: > > class String > def stripQuotes > s = sub( '^\"', " " ) > s.sub!( '\"$', " " ) > return s.strip! > end > end > > This hardly strikes me as the most elegant way. In particular, a nil is > returned if the string is unquoted, and the only way I can see to get > around this is by adding more regular expressions to check for the > existence of quotes. > > A problem I'm having is that I can't seem to figure out how to get at > the underlying string data (like an array) in a member function. Here are a couple of possible ways to do this (untested for speed (and multi-line-ness (so please modify as needed))) irb(main):035:0> s " \"hi i am a string\" " irb(main):036:0> s.strip.sub(/^"(.*)"$/,'\1') "hi i am a string" irb(main):037:0> s.strip.delete('"') "hi i am a string" The second one is only a good idea if you know that the only " characters will be the ones you want to get rid of. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav