Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove some characters from a string by index (Raku)

Tags:

raku

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
like image 694
Tinmarino Avatar asked Mar 25 '20 22:03

Tinmarino


People also ask

How do I remove a specific character from a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement.

How do I remove the last 3 characters from a string?

slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.

How do you take an element out of a string?

In Python you can use the replace() and translate() methods to specify which characters you want to remove from the string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable. Here is the basic syntax for the replace() method.


1 Answers

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);
like image 93
Sebastian Avatar answered Oct 21 '22 03:10

Sebastian