[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Escape character is strings

Deepu Damodaran

11/26/2007 2:51:00 PM

Hi all,

I'm very new to Ruby. My problem is simply to search for '\' character
from a string. The string is the windows file path. I have to either
escape the '\' character or substitute it with '/'. But String::include?
methods doesn't seems to be helpful. Please see the log.

irb(main):004:0> fold = "abcdefgh/ajf;ldsj\dfd"
=> "abcdefgh/ajf;ldsjdfd"
irb(main):005:0> puts fold.include?("\\")
false
=> nil
irb(main):006:0> puts fold.include?("\")
irb(main):007:1"


so how can we search for the '\' using .include?

Thanks and Regards,
Deepu D
--
Posted via http://www.ruby-....

3 Answers

Christian von Kleist

11/26/2007 3:02:00 PM

0

On Nov 26, 2007 9:51 AM, Deepu Damodaran <deepu.damodaran@wipro.com> wrote:
> Hi all,
>
> I'm very new to Ruby. My problem is simply to search for '\' character
> from a string. The string is the windows file path. I have to either
> escape the '\' character or substitute it with '/'. But String::include?
> methods doesn't seems to be helpful. Please see the log.
>
> irb(main):004:0> fold = "abcdefgh/ajf;ldsj\dfd"
> => "abcdefgh/ajf;ldsjdfd"
> irb(main):005:0> puts fold.include?("\\")
> false
> => nil
> irb(main):006:0> puts fold.include?("\")
> irb(main):007:1"
>
>
> so how can we search for the '\' using .include?
>
> Thanks and Regards,
> Deepu D
> --
> Posted via http://www.ruby-....
>
>

In a quoted expression, the backslash escapes the following character,
so you need to use two backslashes instead, like this:

irb(main):004:0> fold = "abcdefgh/ajf;ldsj\\dfd"
=> "abcdefgh/ajf;ldsj\\dfd"
irb(main):005:0> puts fold.include?("\\")
true
=> nil

Todd Benson

11/26/2007 3:02:00 PM

0

On Nov 26, 2007 8:51 AM, Deepu Damodaran <deepu.damodaran@wipro.com> wrote:
> Hi all,
>
> I'm very new to Ruby. My problem is simply to search for '\' character
> from a string. The string is the windows file path. I have to either
> escape the '\' character or substitute it with '/'. But String::include?
> methods doesn't seems to be helpful. Please see the log.
>
> irb(main):004:0> fold = "abcdefgh/ajf;ldsj\dfd"
> => "abcdefgh/ajf;ldsjdfd"
> irb(main):005:0> puts fold.include?("\\")
> false
> => nil
> irb(main):006:0> puts fold.include?("\")
> irb(main):007:1"
>
>
> so how can we search for the '\' using .include?

Your irb line 5 is just fine. It's the line 4 that seems wrong. To
redo your line 4...

irb> fold = "abcdefgh/ajf;ldsj\\def"

or...

irb> fold = 'abcdefgh/ajf;ldsj\def'

The same goes for your line 6. Your are escaping the second " and
that's why it's asking you for another one.

hth,
Todd

Jacob Dunphy

11/26/2007 6:31:00 PM

0

You can use the integer equivalent of the character as well.

fold.include?(92)

On Nov 26, 6:51 am, Deepu Damodaran <deepu.damoda...@wipro.com> wrote:
> Hi all,
>
> I'm very new to Ruby. My problem is simply to search for '\' character
> from a string. The string is the windows file path. I have to either
> escape the '\' character or substitute it with '/'. But String::include?
> methods doesn't seems to be helpful. Please see the log.
>
> irb(main):004:0> fold = "abcdefgh/ajf;ldsj\dfd"
> => "abcdefgh/ajf;ldsjdfd"
> irb(main):005:0> puts fold.include?("\\")
> false
> => nil
> irb(main):006:0> puts fold.include?("\")
> irb(main):007:1"
>
> so how can we search for the '\' using .include?
>
> Thanks and Regards,
> Deepu D
> --
> Posted viahttp://www.ruby-....