Bug 8181 – String splitting with nonempty delim produces empty result

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-06-01T13:51:00Z
Last change time
2015-05-19T00:12:41Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-06-01T13:51:37Z
Splitting of an empty string according to another nonempty delim string produces an empty array result: import std.stdio, std.string; void main() { writeln("".split(".")); } Output: [] While in Python2 it produces a list (array) that contains an empty string: >>> "".split() [] >>> "".split(".") [''] In most cases such Python string methods are carefully designed. That Python behavour is useful: >>> file_name = "ball.jpg" >>> file_name.split(".")[-1] == "jpg" True >>> file_name = "" >>> file_name.split(".")[-1] == "jpg" False While in D: import std.stdio, std.string; void main() { auto file_name = "ball.jpg"; writeln(file_name.split(".")[$-1] == "jpg"); // Output: true file_name = ""; writeln(file_name.split(".")[$-1] == "jpg"); // range violation } Workaround: import std.stdio, std.array; void main() { auto file_name = ""; writeln(!file_name.empty && file_name.split(".")[$-1] == "jpg"); }
Comment #1 by dlang-bugzilla — 2015-05-19T00:12:41Z
It is too late to change the behavior of split now.