Skip to content

Perl String Functions Guide — Index, Substr, Split, Join, and Formatting

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl String Functions Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl string functions handle text manipulation with index/rindex for substring search, substr for extraction and in-place replacement, split for breaking strings into arrays, and join for array concatenation.

Search Functions

my $text = "The quick brown fox jumps over the lazy dog";

# Find position
my $pos = index($text, "fox");       # 16
my $last = rindex($text, "the");     # 31 (reverse search)

# Search from position
my $pos2 = index($text, "o", 20);    # search starting at 20
my $not_found = index($text, "zebra");  # -1

Substring Operations

my $s = "Hello, World!";

# Extract substring
my $sub = substr($s, 7, 5);          # "World"
my $end = substr($s, 7);             # "World!"
my $last3 = substr($s, -3);          # "ld!"

# Replace in-place
my $copy = $s;
substr($copy, 7, 5, "Perl");
print $copy;  # "Hello, Perl!"

# Remove last character
my $url = "https://example.com/";
substr($url, -1, 1, "");
print $url;   # "https://example.com"

Split and Join

# Split on delimiter
my $csv = "apple,banana,cherry";
my @fruits = split(/,/, $csv);       # ("apple", "banana", "cherry")

# Split with limit
my @parts = split(/,/, $csv, 2);     # ("apple", "banana,cherry")

# Split on whitespace (default)
my $line = "hello   world\tlua";
my @words = split(/\s+/, $line);     # ("hello", "world", "lua")

# Split into characters
my @chars = split(//, "Perl");       # ("P", "e", "r", "l")

# Join
my $new_csv = join(",", @fruits);    # "apple,banana,cherry"

Formatting

# sprintf - format string
my $formatted = sprintf("Price: \$%.2f", 19.995);  # "Price: $20.00"
my $hex = sprintf("0x%X", 255);                    # "0xFF"
my $padded = sprintf("%-10s", "left");             # "left      "

# chr and ord
my $char = chr(65);    # "A"
my $code = ord("A");   # 65

Common Mistakes

1. Off-by-one with substr

substr positions are 0-based. substr($s, 0, 1) gets the first character.

2. Modifying with substr in void context

substr($s, 0, 1, "X") modifies $s. The modified substring is returned if used in non-void context.

3. Not escaping split patterns

split(/./, $s) splits on any character (regex). Use split(/\./, $s) or split(/[.]/, $s) for literal dot.

Practice Questions

1. How do you find the position of a substring? index($string, $substring) returns the position (0-based) or -1 if not found.

2. How do you split a string into an array? my @parts = split(/delimiter/, $string) splits on the regex delimiter.

3. How do you replace a portion of a string? substr($s, $pos, $len, $replacement) replaces $len characters at $pos with $replacement.

FAQ

{{< faq question="What is the difference between index and rindex?" >}} index finds the first occurrence from left. rindex finds the last occurrence (searches from right). {{< /faq >}}

{{< faq question="How do I split on whitespace properly?" >}} split(/\s+/, $string) splits on one or more whitespace characters (spaces, tabs, newlines). {{< /faq >}}

{{< faq question="Can substr be used on the left side of assignment?" >}} No, but substr($s, $pos, $len, $replacement) modifies $s in-place and returns the removed substring. {{< /faq >}}

What's Next

Now learn about sorting techniques in Perl.

Topic Description Link
Sorting Custom comparison and sorting {{< ref "19-sorting" >}}
Map/Grep List processing functions {{< ref "20-maps-filters" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro