简体   繁体   中英

How best to search for the string “%00” in a variable?

I have a Perl variable that contains (linke ther termninal calles) the string "&00". It's possible that the string is present multiple times in the larger string.

How can I discover if the larger string contains the smaller string, to "chop" it? It's always present at the end of the string.

Something like:

if ($string =~ (m//))

Assuming, you want to check or replace just the end of the string,

if ($string =~ /\&00$/)

to detect it.

Or if you just want to replace it,

$string =~ s/(.*)\&00$/$1/

如果您只想在结尾处将其删除:

if ( substr($string, -3) eq '&00' )

You can use the index function .

use strict;
use warnings;

my $sometext = "my text here contains &00 and some more text";
my $text_to_search = "&00";
print index($sometext, $text_to_search), "\n";

You can later use substr to chop it off.

my $idx = index($sometext, $text_to_search);
print substr($sometext, 0, $idx); # if it's to the end, you can alter to suit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM