3

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.

Samuel Lampa
  • 4,336
  • 5
  • 42
  • 63

2 Answers2

5

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");
}
Adam D. Ruppe
  • 25,382
  • 4
  • 41
  • 60
1

I gues also this is working:

string temp = "sometemp";
assert(temp[0..2] == "so")
Burak Tutanlar
  • 140
  • 2
  • 11