Swift:字符串包含字符串(不使用 NSString)? [英] Swift: String contains String (Without using NSString)?

查看:33
本文介绍了Swift:字符串包含字符串(不使用 NSString)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了和这个问题一样的问题:

I have the same problem like in this question:

我如何检查是否一个字符串在 Swift 中包含另一个字符串?

但现在几个月后我想知道是否可以不使用 NSString 来完成?我很好,简单的包含方法就可以了.我搜索了网络和文档,但一无所获!

But now a few months later I wonder if it can be done without using NSString? I nice and simple contains-method would be fine. I searched the web and the documentation but I found nothing!

推荐答案

同样的方法,只是使用 Swift 语法:

Same way, just with Swift syntax:

let string = "This is a test.  This is only a test"

if string.rangeOfString("only") != nil {
     println("yes")
}

适用于 Swift 3.0

For Swift 3.0

if str.range(of: "abc") != nil{
     print("Got the string")
}

这篇关于Swift:字符串包含字符串(不使用 NSString)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆