in reply to positions of all occurrences of a substring in a string
Hi,
Yes, You need to loop over index() function to find every occurrence of a substring in a string.
Example:
#!/usr/bin/perl use strict; use warnings; my $string = 'every occurrence of a substring in a string'; my $char = 'st'; my $offset = 0; my $result = index($string, $char, $offset); while ($result != -1) { print "Found $char at $result\n"; $offset = $result + 1; $result = index($string, $char, $offset); }
The above code finds the substring 'st' in the string.
Output:Found st at 25
Found st at 37
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: positions of all occurrences of a substring in a string
by Anonymous Monk on Jun 12, 2024 at 20:13 UTC |