繁体   English   中英

如何最好地在变量中搜索字符串“%00”?

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

我有一个Perl变量,该变量包含(链接该终端调用)字符串“&00”。 该字符串可能在较大的字符串中多次出现。

如何发现较大的字符串是否包含较小的字符串,以“斩波”它? 它总是出现在字符串的末尾。

就像是:

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

假设您只想检查或替换字符串的结尾,

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

检测它。

或者,如果您只想更换它,

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

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

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

您可以使用索引功能

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";

以后您可以使用substr对其进行裁剪。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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