发布于 3年前
                Raku根据index删除字符串中的字符
示例说明:0123456789,删除索引为1-3,以及8的字符串,结果应该为:045679。
实现
my $a='0123456789';
with $a {$_=.comb[(^* ∖ (1..3, 8).flat).keys.sort].join};
say $a;可以简写为一行代码:
say '0123456789'.comb[(^* ∖ (1..3, 8).flat).keys.sort].join;抽取公共方法
为了更方便点,可以抽象出一个函数。
方法一:
sub remove($str, $a) {
    $str.comb[(^* ∖ $a.flat).keys.sort].join;
}
say '0123456789'.&remove: (1..3, 8);方法二:直接给Str添加参数:
use MONKEY-TYPING;
augment class Str {
    method remove($a) {
        $.comb[(^* ∖ $a.flat).keys.sort].join;
    }
};
say '0123456789'.remove: (1..3, 8); 
             
             
             
             
            