Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Strings and Sorting > Finding a Substring with index

Finding a Substring with index

Finding a substring depends on where you have lost it. If you happen to have lost it within a bigger string, you’re in luck because the index function can help you out. Here’s how it looks:

$where = index($big, $small);

Perl locates the first occurrence of the small string within the big string, returning an integer location of the first character. The character position returned is a zero-based value—if the substring is found at the very beginning of the string, index returns 0. If it’s one character later, the return value is 1, and so on. If index can’t find the substring at all, it returns –1 to indicate that.[339] In this example, $where gets 6:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint