To replace multiple spaces with a single space in Perl, you can use the s///
operator with a regular expression pattern.
In this example,
s///
operator with the regular expression pattern \s+
to replace multiple spaces with a single space in the string \$str
.\$str
.\$str = "This is a test string";
\$str =~ s/\s+/ /g;
print \$str;
This is a test string
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in Perl language with well detailed examples.