From: Joel VanderWerf Date: 2006-07-06T02:47:24+09:00 Subject: Re: Find Position of text j3n7il jones wrote: > Im trying to recreate a function I had in Delphi for ruby, but cannot > figure out how to search for a string withn a string > > Here is the Delphi code > > function ReplaceString(str, value, replacer: String): String; > var > aPos: Integer; > rslt: String; > begin > aPos := Pos(value, str); > rslt := ''; > result := str;//in case there is nothing to replace, return orig > string > while (aPos <> 0) do begin > rslt := Copy(str, 1, aPos - 1) + replacer; > rslt := rslt + Copy(str, aPos + Length(value), Length(str)); > str := rslt; > aPos := Pos(value, str); > result := rslt; > end; > end; > > As you can see after begin, it calls a pascal function called "pos" > > which just looks for value(a string) within str (another string) and > returns a number, the position of value within str. > > Possible in ruby? > [~] irb irb(main):001:0> s = "Possible in ruby?" => "Possible in ruby?" irb(main):002:0> s.index "in" => 9 irb(main):003:0> s[/in/] = "and easy in" => "and easy in" irb(main):004:0> s => "Possible and easy in ruby?" irb(main):005:0> s[/\?/] = "!" => "!" irb(main):006:0> s => "Possible and easy in ruby!" -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407