In Perl, you can split a string by new line using the split
function with the newline character '\n'
.
In this example,
$str
with a value containing new lines.split
function with the newline character '\n'
to split the string into an array of substrings.$str = "Hello\nWorld\n";
@substrings = split("\n", $str);
foreach $sub (@substrings) {
print $sub . "\n";
}
Hello World
In this tutorial, we learned How to Split a String by New Line in Perl language with well detailed examples.