Str

Str

The Str class is the base primitive class of Spit-sh. It represents a string in the shell. Since all shell constructs are made out of strings all types inherits from this class.

say "foo" ~~ Str; # true
say <one two three> ~~ Str; #true

ACCEPTS

method ACCEPTS(Str $b āŸ¶ Bool)

Returns True if the invocant and argument string are equal.

Bool

method Bool( āŸ¶ Bool)

Returns true if the string isn't empty

bytes

method bytes( āŸ¶ Int)

Returns the number of bytes in the string.

chars

method chars( āŸ¶ Int)

Returns the number of characters in the string. note: This will depend on the locale of the terminal the script is running in.

contains

method contains(Str i āŸ¶ Bool)

Returns true if the string contains $needle.

say "Hello, World".contains('Wo'); #-> True
say "Hello, World".contains('wo'); #-> False
say "Hello, World".contains('wo',:i); #-> True
ParameterDescription
$needleThe string being searched for
:$iTurns on case insensitive matching

ends-with

method ends-with(Str $ends-with āŸ¶ Bool)

Returns true if the string ends with the argument.

my @urls = <github.com ftp://ftp.FreeBSD.org>;
for @urls {
    print "$_ might be: ";
    when .ends-with('.com') { say 'commercial' }
    when .ends-with('.org') { say 'an organisation' }
    when .ends-with('.io')  { say 'a moon of Jupiter' }
}
ParameterDescription
$ends-withTrue if the string ends-with this

lc

method lc( āŸ¶ Str)

Returns an lowercase version of the string

match

method match(Regex $r āŸ¶ Bool)

Returns true if the the string matches the regex and sets the @/ match variable to the match and its capture groups (one per line).

my $regex = rxā€˜^(.+)://([^/]+)/?(.*)$ā€™;
if 'https://github.com/spitsh/spitsh'.match($regex) {
    say @/[0]; #-> https://github.com/spitsh/spitsh
    say @/[1]; #-> https
    say @/[2]; #-> github.com
    say @/[3]; #-> spitsh/spitsh
}
ParameterDescription
$rThe regular expression to match against

matches

method matches(Regex $r āŸ¶ Bool)

Returns true if the string matches the regex and doesn't set or modify @/ match variable.

my $regex = rxā€˜^(.+)://([^/]+)/?(.*)$ā€™;
my $url = 'https://github.com/spitsh/spitsh';
if $url.match($regex) {
    my $host = @/[2];
    if $host.matches(/(www\.)?github.com/) {
        # @/ is preserved.
        my @user-repo = @/[3].split('/');
        say "The owner is @user-repo[0]. The repo is @user-repo[1]";
    } else {
        say "it's not github";
    }
}

note

method note()

Prints the string to stderr

say

method say()

Prints the string to stdout

split

method split(Str $sep āŸ¶ List)

Splits the string on a separator. Returns the string with each instance of the $sep replaced with \n as a [List].

ParameterDescription
$sepThe separator to split on

starts-with

method starts-with(Str $starts-with āŸ¶ Bool)

Returns true if the string starts with the argument.

my @urls = <http://github.com ftp://ftp.FreeBSD.org>;
for @urls {
    print "$_ is:";
    when .starts-with('http') { say "hyper text transfer" }
    when .starts-with('ftp')  { say "file transfer" }
    default { "well I'm not sure.." }
}
ParameterDescription
$starts-withTrue if the string starts-with this

subst

method subst(Str replacement, Bool :$g āŸ¶ Str)

Returns the string with the target string replaced by a replacement string. Does not modify the original string.

my $a = "food";
$a.subst('o','e').say;
$a.subst('o','e',:g).say;
say $a;
ParameterDescription
$targetThe string to be replaced
$replacementThe string to replace it with
:$gTurns on global matching

uc

method uc( āŸ¶ Str)

Returns an uppercase version of the string

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.