From: Logan Capaldo Date: 2006-07-06T02:45:21+09:00 Subject: Re: Find Position of text On Jul 5, 2006, at 1:34 PM, 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? > > -- > Posted via http://www.ruby-forum.com/. > ri String#index Of course ruby already knows how to do this function you are trying to write. def ReplaceString(str, value, replacement) copy_of_str = str.dup copy_of_str[value] = replacement copy_of_str end