【问题标题】:Check if string starts with a substring in D / phobos?检查字符串是否以 D / phobos 中的子字符串开头?
【发布时间】:2013-01-01 19:23:46
【问题描述】:

到目前为止,我还没有找到最容易检查字符串是否以 D 中的某个字符开头的方法。

我想要类似的东西:

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

我找到的最接近的是“chompPrefix”(here),但这并不是我真正想要的。

【问题讨论】:

    标签: d phobos


    【解决方案1】:

    std.algorithm 中有一个 startsWith 可以像那样工作。

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

    【讨论】:

    • 啊,显然我错过了谷歌“startswith site:dlang.org” :) 谢谢!
    【解决方案2】:

    我想这也有效:

    string temp = "sometemp";
    assert(temp[0..2] == "so")
    

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 1970-01-01
      • 2023-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      相关资源
      最近更新 更多