Swift: 如何检查字符串是否包含数组中的任何单词/子字符串

版权声明:本文为陈云峰(www.swifty.cc)原创文章,未经允许不得转载。 https://blog.csdn.net/feosun/article/details/82685244

您应该已经知道可以检查字符串是否包含单个单词,如下所示:

let string = "The rain in Spain" let stringResult = string.contains("rain")

您还应该知道可以检查字符串数组以查看特定字符串是否在那里,如下所示:

let words = ["clouds", "rain", "wind"] let arrayResult = words.contains("rain")

好吧,这两个contains()方法可以组合在一起形成一个新的含义:”这个数组中的任何单词都存在于这个字符串中吗?”,如下所示:

 let combinedResult = words.contains(where: string.contains)

猜你喜欢

转载自blog.csdn.net/feosun/article/details/82685244