in reply to Search string without using regexp

Search and Replace: substr

-- Ken

Replies are listed 'Best First'.
Re^2: Search string without using regexp
by cheekuperl (Monk) on Jun 09, 2012 at 14:55 UTC
    mmmm...i think substr requires prior knowledge of position of substring. index seems to be the correct option to me.
    Please correct me if i am wrong.

      index lets you search a string quickly for an exact match, or find an exact match after some position, or walk the string finding each exact match (by advancing the starting position each time).

      substr will return a substring, which you could then programatically process and test for some condition if you want. If you want to find the first position in a string where a four character sequence looks a particular way after the digits are reversed, you might walk the string using substr, advancing the position after each check (a bad example, since it's cheaper to just reverse the whole string and then use index, subtracting four from the result).

      index is a more specific function, and substr is a more general function that can be incorporated into a more specific solution.


      Dave

      ++davido

      If index() fits the bill for whatever unspecified m/.../ operations you are performing, then substr() might similarly prove useful for s/.../.../ operations.

      -- Ken