我想从字符串中删除尾部斜杠.例如,如果我有一个名为$ test ="test /"的字符串.我怎么能在最后删除斜线?
如果你确定最后有一个/最后你可以使用chop功能:
$test = "test/";
$test = chop($test);
Run Code Online (Sandbox Code Playgroud)
如果你不确定你能做到:
$test = "test/";
$test = $1 if($test=~/(.*)\/$/);
Run Code Online (Sandbox Code Playgroud)
就个人而言,我会改写这个以避免"\"和"/"的混合
$test =~ s|/$||
Run Code Online (Sandbox Code Playgroud)
如果你使用"|" 你不需要引用"/"