在Objective-C检查一个子字符串的代码中NSString是:
NSString *string = @"hello Swift";
NSRange textRange =[string rangeOfString:@"Swift"];
if(textRange.location != NSNotFound)
{
NSLog(@"exists");
}
Run Code Online (Sandbox Code Playgroud)
但是我如何在Swift中做到这一点?
我有以下用Swift 3编写的简单代码:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
Run Code Online (Sandbox Code Playgroud)
从Xcode 9 beta 5,我得到以下警告:
'
substring(to:)'已被弃用:请使用String带有'partial range from'运算符的切片下标.
如何在Swift 4中使用具有部分范围的切片下标?