digitalmars.D.bugs - [Issue 8181] New: String splitting with nonempty delim produces empty result
- d-bugmail puremagic.com (46/52) Jun 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=8181
http://d.puremagic.com/issues/show_bug.cgi?id=8181 Summary: String splitting with nonempty delim produces empty result Product: D Version: D2 Platform: x86 OS/Version: Windows Status: NEW Severity: normal Priority: P2 Component: Phobos AssignedTo: nobody puremagic.com ReportedBy: bearophile_hugs eml.cc --- Comment #0 from bearophile_hugs eml.cc 2012-06-01 13:51:37 PDT --- 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()[''] In most cases such Python string methods are carefully designed. That Python behavour is useful:"".split(".")Truefile_name = "ball.jpg" 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"); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------file_name = "" file_name.split(".")[-1] == "jpg"
Jun 01 2012