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?

···

--
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

···

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/\.

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

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

···

On 7/5/06, j3n7il jones <jamescnewsom@gmail.com> 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

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Hi --

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?

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 Thu, 6 Jul 2006, j3n7il jones wrote:

--
  "To fully realize the potential of Rails, it's crucial that you take
    the time to fully understand Ruby--and with "Ruby for Rails" David
       has provided just what you need to help you achieve that goal."
       -- DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.
  Complete foreword & sample chapters at http://www.manning.com/black\!

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 Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
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.

···

On Thu, 6 Jul 2006, j3n7il jones wrote:

aPos := Pos(value, str);

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?

class String - RDoc Documentation may be of interest

unknown wrote:

Hi --

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:

···

On Thu, 6 Jul 2006, j3n7il jones wrote:

--
Posted via http://www.ruby-forum.com/\.

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?

···

--
Posted via http://www.ruby-forum.com/.

j3n7il jones wrote:

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?

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.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

-austin

···

On 7/5/06, j3n7il jones <jamescnewsom@gmail.com> wrote:

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

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

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

-Mat

···

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

j3n7il jones wrote:

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?

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.

Mat Schaffer 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!

--
Posted via http://www.ruby-forum.com/\.