subtracting strings

  • Thread starter Gunnar Hjalmarsson
  • Start date
G

Gunnar Hjalmarsson

Dan said:
What's the generally accepted or most efficient way to "subtract"
one string from another, or to remove a substring from a string.
In other words, something like this:

my $string = "This is a really neat string.";
my $stringtosub = "really neat ";

#doesn't work
my $newstring = $string - $stringtosub;
#$newstring would now be "This is a string."

(my $newstring = $string) =~ s/$stringtosub//;
 
D

Dan Jones

What's the generally accepted or most efficient way to "subtract" one string
from another, or to remove a substring from a string. In other words,
something like this:

my $string = "This is a really neat string.";
my $stringtosub = "really neat ";

#doesn't work
my $newstring = $string - $stringtosub;
#$newstring would now be "This is a string."

If $stringtosub works as a regex, I could do it with backrefs but
$stringtosub may contain characters which would need to be escaped in a
regex.
 
G

Gunnar Hjalmarsson

Gunnar said:
(my $newstring = $string) =~ s/$stringtosub//;

It struck me that as long as we are talking about plain strings, it's
not necessary to involve the regex engine:

substr $string, index($string, $stringtosub),
length $stringtosub, '';

print "$string\n";

While it should be noted that $string is changed with that solution,
it may be more efficient than using the s/// operator. (But I haven't
benchmarked it, so I shouldn't really say that...)
 
J

Jürgen Exner

Dan said:
What's the generally accepted or most efficient way to "subtract" one
string from another, or to remove a substring from a string. In
other words, something like this:

my $string = "This is a really neat string.";
my $stringtosub = "really neat ";

#doesn't work
my $newstring = $string - $stringtosub;

Well, no. This will typically yield 0-0 which assigns 0 to $newstring.
#$newstring would now be "This is a string."

If $stringtosub works as a regex, I could do it with backrefs but

You don't need backrefs, just a good old plain
$string =~ s/$stringtosub//;
$stringtosub may contain characters which would need to be escaped in
a regex.

Yep, that's a problem. But you may want to take a look at the "qr" operator.

A totally different approach would be to use index() to search for the
position of the substring and then substr() to remove that section.

I don't know which one would be faster, but REs are usually quite expensive.

jue
 
M

Michele Dondi

(my $newstring = $string) =~ s/$stringtosub//;

If doing it this way, then

(my $newstring = $string) =~ s/\Q$stringtosub//;

would definitely be better...


Michele
 
B

Ben Morrow

Quoth "Jürgen Exner said:
Yep, that's a problem. But you may want to take a look at the "qr" operator.

ITYM 'quotemeta' or the \Q regex escape?

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top