Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Check if one string is a substring of another.

by Anonymous Monk
on Feb 14, 2006 at 10:03 UTC ( [id://530070]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Suppose I have two strings, one is longer than the other.
my $s1 = 'foofoo'; my $s2 = 'foo'
What's the quickest way to test that the shorter string is the substring of the longer one?

Replies are listed 'Best First'.
Re: Check if one string is a substring of another.
by davorg (Chancellor) on Feb 14, 2006 at 10:13 UTC

    That's what the index function is for. As long as you are comparing fixed strings then using regular expressions is overkill. Stick with index.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Check if one string is a substring of another.
by izut (Chaplain) on Feb 14, 2006 at 10:15 UTC
    I think you're looking for index. From perldoc -f index:
           index STR,SUBSTR,POSITION
           index STR,SUBSTR
                   The index function searches for one string within another, but
                   without the wildcard-like behavior of a full regular-expression
                   pattern match.  It returns the position of the first occurrence
                   of SUBSTR in STR at or after POSITION.  If POSITION is omitted,
                   starts searching from the beginning of the string.  The return
                   value is based at 0 (or whatever you'e set the $[ variable
                   to--but don't do that).  If the substring is not found, returns
                   one less than the base, ordinarily "-1".
    

    Igor 'izut' Sutton
    your code, your rules.

Re: Check if one string is a substring of another.
by jbrugger (Parson) on Feb 14, 2006 at 10:18 UTC
    To add to the next comments, if you want to see what string is longer, use length  length $st1 so you can check the longest one, as i understand you need that

    #so you get something like this: my $index = 0; if ( lenght $str1 < length $str2 ) { $index= index $str2,str1; } else { $index= index $str1,str2; }


    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Check if one string is a substring of another.
by mk. (Friar) on Feb 14, 2006 at 11:36 UTC
    perl -e '$s1="foofoo";$s2="foo";(length$s1<length$s2)?$check=index$s2,$s1:$check=index$s1,$s2;print "match\n" if $check>=0'


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    @
Re: Check if one string is a substring of another.
by ayrnieu (Beadle) on Feb 14, 2006 at 10:06 UTC
    $s1 =~ /\Q$s2/

    Updated: oh, if you don't know which is shorter:

    length($s1)>length($s2) ? $s1 =~ /\Q$s2/ : $s2 =~ /\Q$s1/;
Re: Check if one string is a substring of another.
by prasadbabu (Prior) on Feb 14, 2006 at 10:08 UTC

    In terms of regular expression, here is one way. Here i assume $s1 as longer string. If $s2 is longer, change the below code as vice versa.

    my $s1 = 'foofoo'; my $s2 = 'foo'; print "substring" if ($s1 =~ /\Q$s2\E/); #if $s1 is longer print "substring" if ($s2 =~ /\Q$s1\E/); #if $s2 is longer

    updated: Eventhough regex is one way, as davorg suggested index is the best solution for this.

    Prasad

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://530070]
Approved by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (None)
    As of 2024-04-25 01:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found