www.digitalmars.com         C & C++   DMDScript  

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


           Summary: String splitting with nonempty delim produces empty
           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() {


While in Python2 it produces a list (array) that contains an empty string:

[''] In most cases such Python string methods are carefully designed. That Python behavour is useful:
 file_name = "ball.jpg"
 file_name.split(".")[-1] == "jpg"
 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