Pergunta

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.

Foi útil?

Solução

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");
}

Outras dicas

I gues also this is working:

string temp = "sometemp";
assert(temp[0..2] == "so")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top