Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions std/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,9 @@ $(D @safe), $(D pure) and $(D CTFE)-able.
Params:
s = the string to split

Returns:
An array of each word in `s`

See_Also:
$(REF splitter, std,algorithm,iteration) for a version that splits using any
separator.
Expand Down Expand Up @@ -1402,6 +1405,29 @@ if (isSomeString!S)
return result;
}

///
@safe unittest
{
string str = "Hello World!";
assert(str.split == ["Hello", "World!"]);

string str2 = "Hello\t\tWorld\t!";
assert(str2.split == ["Hello", "World", "!"]);
}

/**
* `split` allocates memory, so the same effect can be achieved lazily
* using $(REF splitter, std,algorithm,iteration).
*/
@safe unittest
{
import std.ascii : isWhite;
import std.algorithm.comparison : equal;

string str = "Hello World!";
assert(str.splitter!(isWhite).equal(["Hello", "World!"]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: you wouldn't need the braces around isWhite here - see it.
(I don't mind if they are added for extra clarity)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, they're just there to make things clearer. I always add them in a UFCS chain.

}

@safe unittest
{
import std.conv : to;
Expand Down