如何检查数组中的字符串是否出现?我的意思是我可以循环,但是有标准功能吗?
起初我做了:
if(str in ["first", "second", "third"])
Run Code Online (Sandbox Code Playgroud)
但它抱怨in只适用于关联数组.
我试图快速查找phobos文档,但没有找到任何与数组相关的模块.
那么有什么,或者我只需要手动循环它?
我在D1,phobos.
如果您的字符串是常量(如示例中所示),您可以使用关联数组文字,但语法并不漂亮:
if (str in ["first"[]:0, "second":0, "third":0])
Run Code Online (Sandbox Code Playgroud)
我不认为有一个库调用您可以在D1的火卫一用,但D2的std.algorithm有东西,你可以使用:
if (count(["first", "second", "third"][], str))
Run Code Online (Sandbox Code Playgroud)
在Tango中,您可以使用以下通用contains函数tango.text.Util:
if (contains(["first", "second", "third"][], str))
Run Code Online (Sandbox Code Playgroud)
请注意,[]数组文字的末尾是必需的,因为我们需要传递静态数组的内存片,而不是实际的静态数组的值.