Remove some characters from a string by index (Raku)

Tinmarino :

FAQ: In Raku, how do you remove some characters from a string, based on their index?

Say I want to remove indices 1 to 3 and 8

xxx("0123456789", (1..3, 8).flat);  # 045679
Sebastian :

Variant of Shnipersons answer:

my $a='0123456789';
with $a {$_=.comb[(^* ∖ (1..3, 8).flat).keys.sort].join};
say $a;

In one line:

say '0123456789'.comb[(^* ∖ (1..3, 8).flat).keys.sort].join;

or called by a function:

sub remove($str, $a) {
    $str.comb[(^* ∖ $a.flat).keys.sort].join;
}

say '0123456789'.&remove: (1..3, 8);

or with augmentation of Str:

use MONKEY-TYPING;
augment class Str {
    method remove($a) {
        $.comb[(^* ∖ $a.flat).keys.sort].join;
    }
};

say '0123456789'.remove: (1..3, 8);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=375552&siteId=1