www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8181] New: String splitting with nonempty delim produces empty result

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



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"); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 01 2012