Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何查找Swift字符串中最后出现的子字符串?_String_Swift_Substring - Fatal编程技术网

String 如何查找Swift字符串中最后出现的子字符串?

String 如何查找Swift字符串中最后出现的子字符串?,string,swift,substring,String,Swift,Substring,在Objective-C中,我使用了: [@"abc def ghi abc def ghi" rangeOfString:@"c" options:NSBackwardsSearch]; 但现在NSBackardsSearch似乎不存在。有人能为Swift提供同等的代码吗 如果可能的话,我希望能够在整个字符串中找到字符号。因此,在上面的示例中,它将返回3。Cocoa框架应该可以在Swift中访问,但您需要导入它们。尝试导入Foundation以访问NSString API。从“将Swift与

在Objective-C中,我使用了:

[@"abc def ghi abc def ghi" rangeOfString:@"c" options:NSBackwardsSearch];
但现在NSBackardsSearch似乎不存在。有人能为Swift提供同等的代码吗


如果可能的话,我希望能够在整个字符串中找到字符号。因此,在上面的示例中,它将返回3。

Cocoa框架应该可以在Swift中访问,但您需要导入它们。尝试导入
Foundation
以访问NSString API。从“将Swift与Cocoa和Objective-C一起使用”指南的“”部分:

Swift自动在字符串类型和NSString类之间建立桥接。(…)启用字符串桥接,只需导入基础。

此外,
nsbackardssearch
是一个枚举值(标记并导入为),因此您必须使用Swift的枚举/选项语法来访问它(作为
NSStringCompareOptions
选项类型的一部分)。前缀从值中剥离,因此从值名称中删除
NS

综上所述,我们有:

import Foundation
"abc def ghi abc def ghi".rangeOfString("c", options:NSStringCompareOptions.BackwardsSearch)

请注意,您可能必须使用函数来正确使用范围,从
rangeOfString

Swift 3.0:

"abc def ghi abc def ghi".range(of: "c", options: String.CompareOptions.backwards, range: nil, locale: nil)
"abc def ghi abc def ghi".range(of: "c", options: NSString.CompareOptions.backwards)

如果要替换字符串中的最后一个子字符串:

(Swift 3)

用法:

let alphabet = "abc def ghi abc def ghi"
let result = alphabet.replacingLastOccurrenceOfString("ghi",
        with: "foo")

print(result)

// "abc def ghi abc def foo"
或者,如果要完全删除最后一个子字符串并将其清理干净:

let result = alphabet.replacingLastOccurrenceOfString("ghi",
            with: "").trimmingCharacters(in: .whitespaces)

print(result)

// "abc def ghi abc def"

Swift 5:

"abc def ghi abc def ghi".range(of: "c", options: String.CompareOptions.backwards, range: nil, locale: nil)
"abc def ghi abc def ghi".range(of: "c", options: NSString.CompareOptions.backwards)

在Swift 4.2中也能很好地工作。非常感谢您的帮助。