In Cool§
See primary documentation in context for routine split
multi split( Str , Str(Cool) , = Inf, :, :, :, :, :)multi split(Regex , Str(Cool) , = Inf, :, :, :, :, :)multi split(, Str(Cool) , = Inf, :, :, :, :, :)multi method split( Str , = Inf, :, :, :, :, :)multi method split(Regex , = Inf, :, :, :, :, :)multi method split(, = Inf, :, :, :, :, :)
Coerces the invocant (or in the sub form, the second argument) to Str
, splits it into pieces based on delimiters found in the string and returns the result as a Seq
.
If $delimiter
is a string, it is searched for literally and not treated as a regex. You can also provide multiple delimiters by specifying them as a list, which can mix Cool
and Regex
objects.
say split(';', "a;b;c").raku; # OUTPUT: «("a", "b", "c").Seq»say split(';', "a;b;c", 2).raku; # OUTPUT: «("a", "b;c").Seq»say split(';', "a;b;c,d").raku; # OUTPUT: «("a", "b", "c,d").Seq»say split(/\;/, "a;b;c,d").raku; # OUTPUT: «("a", "b", "c,d").Seq»say split(//, "a;b;c,d").raku; # OUTPUT: «("a", "b", "c", "d").Seq»say split(['a', /b+/, 4], '1a2bb345').raku; # OUTPUT: «("1", "2", "3", "5").Seq»
By default, split
omits the matches, and returns a list of only those parts of the string that did not match. Specifying one of the :k, :v, :kv, :p
adverbs changes that. Think of the matches as a list that is interleaved with the non-matching parts.
The :v
interleaves the values of that list, which will be either Match
objects, if a Regex
was used as a matcher in the split, or Str
objects, if a Cool
was used as matcher. If multiple delimiters are specified, Match
objects will be generated for all of them, unless all of the delimiters are Cool
.
say 'abc'.split(/b/, :v); # OUTPUT: «(a 「b」 c)»say 'abc'.split('b', :v); # OUTPUT: «(a b c)»
:k
interleaves the keys, that is, the indexes:
say 'abc'.split(/b/, :k); # OUTPUT: «(a 0 c)»
:kv
adds both indexes and matches:
say 'abc'.split(/b/, :kv); # OUTPUT: «(a 0 「b」 c)»
and :p
adds them as Pair
s, using the same types for values as :v
does:
say 'abc'.split(/b/, :p); # OUTPUT: «(a 0 => 「b」 c)»say 'abc'.split('b', :p); # OUTPUT: «(a 0 => b c)»
You can only use one of the :k, :v, :kv, :p
adverbs in a single call to split
.
Note that empty chunks are not removed from the result list. For that behavior, use the :skip-empty
named argument:
say ("f,,b,c,d".split: /","/ ).raku; # OUTPUT: «("f", "", "b", "c", "d").Seq»say ("f,,b,c,d".split: /","/, :skip-empty).raku; # OUTPUT: «("f", "b", "c", "d").Seq»
In IO::Path§
See primary documentation in context for method split
method split(IO::Path: |args --> Seq)
Opens the file and processes its contents the same way Str.split
does, taking the same arguments. Implementations may slurp the file in its entirety when this method is called.
In IO::Spec::Unix§
See primary documentation in context for method split
method split(IO::Spec::Unix: Cool )
Creates an IO::Path::Parts
for $path
, with an empty string as its volume
attribute's value.
IO::Spec::Unix.split('C:/foo/bar.txt').raku.say;# OUTPUT: «IO::Path::Parts.new("","C:/foo","bar.txt")»IO::Spec::Unix.split('/foo/').raku.say;# OUTPUT: «IO::Path::Parts.new("","/","foo")»IO::Spec::Unix.split('///').raku.say;# OUTPUT: «IO::Path::Parts.new("","/","/")»IO::Spec::Unix.split('./').raku.say;# OUTPUT: «IO::Path::Parts.new("",".",".")»IO::Spec::Unix.split('.').raku.say;# OUTPUT: «IO::Path::Parts.new("",".",".")»IO::Spec::Unix.split('').raku.say;# OUTPUT: «IO::Path::Parts.new("","","")»
Note: Before Rakudo version 2020.06 this method split the given $path
into "volume", "dirname", and "basename" and returned the result as a List
of three Pair
s, in that order.
In IO::Spec::Cygwin§
See primary documentation in context for method split
method split(IO::Spec::Cygwin: Cool )
Same as IO::Spec::Win32.split
, except it replaces backslashes with slashes in all the values of the final result.
In Supply§
See primary documentation in context for method split
multi method split(Supply: \delimiter)multi method split(Supply: \delimiter, \limit)
This method creates a supply of the values returned by the Str.split
method called on the string collected from the invocant. See Str.split
for details on the \delimiter
argument as well as available extra named parameters. The created supply can be limited with the \limit
argument, see .head
.
my = Supply.from-list(<Hello World From Raku!>);my = .split(/ /, 2, :skip-empty);.tap(); # OUTPUT: «HelloWorld»
In IO::CatHandle§
See primary documentation in context for method split
method split(IO::CatHandle: |args --> Seq)
Read the handle and processes its contents the same way Str.split
does, taking the same arguments. Implementations may slurp the contents of all the source handles in their entirety when this method is called.
(my = 'foo'.IO).spurt: 'foo';(my = 'bar'.IO).spurt: 'bar';IO::CatHandle.new(, ).split(/o+/).raku.say;# OUTPUT: «("f", "bar").Seq»
In Str§
See primary documentation in context for routine split
multi split( Str , Str , = Inf,:, :, :, :, :)
multi split(Regex , Str , = Inf,:, :, :, :, :)
multi split(List , Str , = Inf,:, :, :, :, :)
multi method split(Str: Str , = Inf,:, :, :, :, :)
multi method split(Str: Regex , = Inf,:, :, :, :, :)
multi method split(Str: List , = Inf,:, :, :, :, :)
Splits a string up into pieces based on delimiters found in the string.
If DELIMITER
is a string, it is searched for literally and not treated as a regex. If DELIMITER
is the empty string, it effectively returns all characters of the string separately (plus an empty string at the begin and at the end). If PATTERN
is a regular expression, then that will be used to split up the string. If DELIMITERS
is a list, then all of its elements will be considered a delimiter (either a string or a regular expression) to split the string on.
The optional LIMIT
indicates in how many segments the string should be split, if possible. It defaults to Inf (or *, whichever way you look at it), which means "as many as possible". Note that specifying negative limits will not produce any meaningful results.
A number of optional named parameters can be specified, which alter the result being returned. The :v
, :k
, :kv
and :p
named parameters all perform a special action with regards to the delimiter found.
:skip-empty
If specified, do not return empty strings before or after a delimiter.
:v
Also return the delimiter. If the delimiter was a regular expression, then this will be the associated Match
object. Since this stringifies as the delimiter string found, you can always assume it is the delimiter string if you're not interested in further information about that particular match.
:k
Also return the index of the delimiter. Only makes sense if a list of delimiters was specified: in all other cases, this will be 0.
:kv
Also return both the index of the delimiter, as well as the delimiter.
:p
Also return the index of the delimiter and the delimiter as a Pair
.
Examples:
say split(";", "a;b;c").raku; # OUTPUT: «("a", "b", "c").Seq»say split(";", "a;b;c", :v).raku; # OUTPUT: «("a", ";", "b", ";", "c").Seq»say split(";", "a;b;c", 2).raku; # OUTPUT: «("a", "b;c").Seq»say split(";", "a;b;c", 2, :v).raku; # OUTPUT: «("a", ";", "b;c").Seq»say split(";", "a;b;c,d").raku; # OUTPUT: «("a", "b", "c,d").Seq»say split(/\;/, "a;b;c,d").raku; # OUTPUT: «("a", "b", "c,d").Seq»say split(<; ,>, "a;b;c,d").raku; # OUTPUT: «("a", "b", "c", "d").Seq»say split(//, "a;b;c,d").raku; # OUTPUT: «("a", "b", "c", "d").Seq»say split(<; ,>, "a;b;c,d", :k).raku; # OUTPUT: «("a", 0, "b", 0, "c", 1, "d").Seq»say split(<; ,>, "a;b;c,d", :kv).raku; # OUTPUT: «("a", 0, ";", "b", 0, ";", "c", 1, ",", "d").Seq»say "".split("x").raku; # OUTPUT: «("",).Seq»say "".split("x", :skip-empty).raku; # OUTPUT: «().Seq»say "abcde".split("").raku; # OUTPUT: «("", "a", "b", "c", "d", "e", "").Seq»say "abcde".split("",:skip-empty).raku; # OUTPUT: «("a", "b", "c", "d", "e").Seq»
In IO::Spec::Win32§
See primary documentation in context for method split
method split(IO::Spec::Win32: Cool )
Creates an IO::Path::Parts
for $path
.
IO::Spec::Win32.split('C:/foo/bar.txt').raku.say;# OUTPUT: «IO::Path::Parts.new("C:","/foo","bar.txt")»IO::Spec::Win32.split('/foo/').raku.say;# OUTPUT: «IO::Path::Parts.new("","/","foo")»IO::Spec::Win32.split('///').raku.say;# OUTPUT: «IO::Path::Parts.new("","/","\\")»IO::Spec::Win32.split('./').raku.say;# OUTPUT: «IO::Path::Parts.new("",".",".")»IO::Spec::Win32.split('.').raku.say;# OUTPUT: «IO::Path::Parts.new("",".",".")»IO::Spec::Win32.split('').raku.say;# OUTPUT: «IO::Path::Parts.new("","","")»
Note: Before Rakudo version 2020.06 this method split the given $path
into "volume", "dirname", and "basename" and returned the result as a List
of three Pair
s, in that order.
In Allomorph§
See primary documentation in context for method split
method split(Allomorph: |c)
Calls Str.split
on the invocant's Str
value.
In IO::Handle§
See primary documentation in context for method split
method split(IO::Handle: :, |c)
Slurps the handle's content and calls Str.split
on it, forwarding any of the given arguments. If :$close
named parameter is set to True
, will close the invocant after slurping.
Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode
exception being thrown.
my = 'path/to/file'.IO.open;.split: '♥', :close; # Returns file content split on ♥