In Perl, you can split a string using the split
function.
In this example,
$str
with a value of "Hello World"
.split
function with a space as the delimiter.$str = 'Hello World';
@tokens = split / /, $str;
foreach $token (@tokens) {
print "$token\n";
}
Hello World
In this example,
$str
with a value of "apple,banana,cherry"
.split
function with a comma as the delimiter.$str = 'apple,banana,cherry';
@tokens = split /,/, $str;
foreach $token (@tokens) {
print "$token\n";
}
apple banana cherry
In this tutorial, we learned How to Split a String in Perl language with well detailed examples.