dphobos

Check if string starts with a substring in D / phobos?


I haven't so far found how I can most easily check if a string starts with a certain character in D.

I want something like:

if (my_str.startswith("/")) {
    // Do something
}

The closest I found was "chompPrefix" (here), but that is not really what I want.


Solution

  • There's a startsWith in std.algorithm that will work just like that.

    import std.algorithm;
    import std.stdio;
    void main() {
        string my_str = "/test";
        if(my_str.startsWith("/"))
            writeln("cool");
    }