Find Position of text

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?

On Jul 5, 2006, at 1:34 PM, j3n7il jones wrote:

aPos := Pos(value, str);
end;

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

j3n7il jones wrote:

aPos := Pos(value, str);
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!”

Hi –

On Thu, 6 Jul 2006, j3n7il jones wrote:

aPos := Pos(value, str);
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?

Of course – and there’s even a built-in method that does it :slight_smile:
Actually a couple of them. Check out String#index and String#rindex.

str = “This is a string.”
str.index(“is”) # 2
str.rindex(“is”) # 5 (i.e., last occurence)
str.index(/i./) # 2 (using regex)

The =~ method/operator also returns an index.

David

On 7/5/06, j3n7il jones [email protected] 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

You probably don’t want to do it the way you’re doing it now.

a = “abcdef”
a.sub(/cd/, “xy”) # non-destructive, returns a new string

But to your question, =~ works quite well for “pos”, as does #index:

a =~ /cd/ # returns 2
a.index(“cd”)

There’s a lot more to strings and regexen than Pascal ever dreamed.

-austin

Is thier also a way to go to the next occurence?

Like

input = : cat : bat : mat :

param = getparam(input,’:’, 3)

then param would equal “mat”

See what I Mean?

unknown wrote:

Hi –

On Thu, 6 Jul 2006, j3n7il jones wrote:

aPos := Pos(value, str);
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?

Of course – and there’s even a built-in method that does it :slight_smile:
Actually a couple of them. Check out String#index and String#rindex.

str = “This is a string.”
str.index(“is”) # 2
str.rindex(“is”) # 5 (i.e., last occurence)
str.index(/i./) # 2 (using regex)

The =~ method/operator also returns an index.

David

Thank you all for the quick responses, It’s going to take some time to
kill old habbits and learn new syntax, but it seems that it would be
more powerfull in the long run. Everything so far is non-logical to me,
but after some experaments im sure it will all come together :slight_smile:

j3n7il jones wrote:

See what I Mean?

Lots of ways to do this. One way:

irb(main):016:0> input
=> “: cat : bat : mat :”
irb(main):017:0> input.scan(/: ([^:]*) /)[2]
=> [“mat”]

The hard part is getting the regex right.

On Jul 5, 2006, at 3:39 PM, Joel VanderWerf wrote:

irb(main):016:0> input
=> “: cat : bat : mat :”
irb(main):017:0> input.scan(/: ([^:]*) /)[2]
=> [“mat”]

The hard part is getting the regex right.

Regex are fun and all, but why not just:
input = “: cat : bat : mat :”
input.split(":")[4].strip

-Mat

On 7/5/06, j3n7il jones [email protected] wrote:

Is thier also a way to go to the next occurence?

Look up #scan and #index (ri String#scan ; ri String#index).

-austin

On Thu, 6 Jul 2006, j3n7il jones wrote:

aPos := Pos(value, str);

I won’t add to the large number of perfectly acceptable replies you have
already…

I just want to make an observation.

I used to use “Pos” like functions in other languages often.

I can’t remember when last I ever used it in Ruby.

There has always been better ways, usually much better ways, of doing
something in Ruby than finding the position of a string in a string.

Curious that.

John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand

Carter’s Clarification of Murphy’s Law.

“Things only ever go right so that they may go more spectacularly wrong
later.”

From this principle, all of life and physics may be deduced.

j3n7il jones wrote:

aPos := Pos(value, str);
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?

class String - RDoc Documentation may be of
interest

Mat S. wrote:

On Jul 5, 2006, at 3:39 PM, Joel VanderWerf wrote:

irb(main):016:0> input
=> “: cat : bat : mat :”
irb(main):017:0> input.scan(/: ([^:]*) /)[2]
=> [“mat”]

The hard part is getting the regex right.

Regex are fun and all, but why not just:
input = “: cat : bat : mat :”
input.split(":")[4].strip

-Mat

Yes mat that is exactly what im looking for!
In IRC the output of commands are always in the same order, I know which
chunk i need and that would work perfectly!