Skip to content

Commit

Permalink
Adding Solution-2 for Removing leading and trailing spaces in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
ppant committed Jan 11, 2012
1 parent b3fe8df commit de0f4da
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Tip-1.pl
@@ -1,6 +1,9 @@
#!/usr/bin/perl -w
use strict;
use
use warnings;
use 5.010;
use Data::Dumper;

# This script will provide the solution for removing leading and trailing spaces in a string
# Solution -1
# These substitutions are very handly when you do comparisons. So it's always advisable to remove these extra space before you check for equality
Expand All @@ -10,4 +13,9 @@
# $ will check for spaces from end of the string and will replace with blank
$test_str =~ s/\s+$//;
# result string can be used for further opeartions
print $test_str;
say $test_str;
# Solution - 2
# We can combine both regaular expressions in one
$test_str =~ s/(^\s+|\s+$)//g;
# !Caution: Capuring might cost you some execution time .. noncapuring regx should spped up
say $test_str;

0 comments on commit de0f4da

Please sign in to comment.