Perl

  1. Home
  2. Computing & Technology
  3. Perl

Perl string index() function - Quick Tutorial

How to use the string index() function

From About.com

$INDEX = index($STRING, $SUBSTRING);
Perl's index() function takes a string and a substring (another string). It will return the position value of the first occurance of the substring within the string.

This is similar to a regular expression call, but without all the overhead of the regex search patterns. It simply returns the index value of the substring if it exists within the string.

$index = index('barfoobar', 'foo');
print $index . "\n";
This will print 3, which is the index value of the start of the substring foo. Note that you can also use it to determine whether or not a string is simply present within another string. If it isn't, the index function will return a value of -1 like so:
$index = index('barfoobar', 'bob');
print $index . "\n";
More Perl Quick Tips

Explore Perl

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Perl

  1. Home
  2. Computing & Technology
  3. Perl
  4. Programming Perl
  5. Perl index function reference - learn how to use Perl's index() function in this quick tutorial.

©2009 About.com, a part of The New York Times Company.

All rights reserved.