使用Swift字符串中子字符串的索引


74

我曾经在JavaScript中这样做:

var domains = "abcde".substring(0, "abcde".indexOf("cd")) // Returns "ab"

Swift没有此功能,如何做类似的事情?


@ eric-d这不是您提到的副本的副本。OP是关于indexOf()而不是substring()的。
mdupls

在Swift 2中,有一个String.rangeOfString(String)方法返回一个Range。
mdupls

Answers:


128

编辑/更新:

Xcode 11.4•Swift 5.2或更高版本

import Foundation

extension StringProtocol {
    func index<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
        range(of: string, options: options)?.lowerBound
    }
    func endIndex<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
        range(of: string, options: options)?.upperBound
    }
    func indices<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Index] {
        ranges(of: string, options: options).map(\.lowerBound)
    }
    func ranges<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Range<Index>] {
        var result: [Range<Index>] = []
        var startIndex = self.startIndex
        while startIndex < endIndex,
            let range = self[startIndex...]
                .range(of: string, options: options) {
                result.append(range)
                startIndex = range.lowerBound < range.upperBound ? range.upperBound :
                    index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
        }
        return result
    }
}

用法:

let str = "abcde"
if let index = str.index(of: "cd") {
    let substring = str[..<index]   // ab
    let string = String(substring)
    print(string)  // "ab\n"
}

let str = "Hello, playground, playground, playground"
str.index(of: "play")      // 7
str.endIndex(of: "play")   // 11
str.indices(of: "play")    // [7, 19, 31]
str.ranges(of: "play")     // [{lowerBound 7, upperBound 11}, {lowerBound 19, upperBound 23}, {lowerBound 31, upperBound 35}]

不区分大小写的样本

let query = "Play"
let ranges = str.ranges(of: query, options: .caseInsensitive)
let matches = ranges.map { str[$0] }   //
print(matches)  // ["play", "play", "play"]

正则表达式样本

let query = "play"
let escapedQuery = NSRegularExpression.escapedPattern(for: query)
let pattern = "\\b\(escapedQuery)\\w+"  // matches any word that starts with "play" prefix

let ranges = str.ranges(of: pattern, options: .regularExpression)
let matches = ranges.map { str[$0] }

print(matches) //  ["playground", "playground", "playground"]

2
这是不完全正确,因为"ab".indexOf("a")"ab".indexOf("c")这两个回报0
格雷厄姆

对于已升级到Swift 3.0的用户:扩展字符串{func indexOf(string:String)-> String.Index?{return range(of:string,options:.literal,range:nil,locale:nil)?. lowerBound}}
gammachill

1
确保您 import Foundation 或这将不起作用。因为实际上您此时只在使用NSString。
CommaToast

range: nil 并且 locale: nil可以省略,这些参数具有默认值nil
马丁R

这是大量的工作,而不是Swift的本机方法。参见下面的@Inder Kumar Rathore的答案-简单使用'.range(of:“ text”)'方法
Marchy

52

使用String[Range<String.Index>]下标可以获得子字符串。您需要起始索引和最后一个索引来创建范围,您可以按以下方式进行操作

let str = "abcde"
if let range = str.range(of: "cd") {
  let substring = str[..<range.lowerBound] // or str[str.startIndex..<range.lowerBound]
  print(substring)  // Prints ab
}
else {
  print("String not present")
}

如果您没有定义此操作符..<的起始索引,它将使用起始索引。您也可以使用str[str.startIndex..<range.lowerBound]代替str[..<range.lowerBound]


15

迅捷5

查找子串的索引

let str = "abcdecd"
if let range: Range<String.Index> = str.range(of: "cd") {
    let index: Int = str.distance(from: str.startIndex, to: range.lowerBound)
    print("index: ", index) //index: 2
}
else {
    print("substring not found")
}

查找字符索引

let str = "abcdecd"
if let firstIndex = str.firstIndex(of: "c") {
    let index = str.distance(from: str.startIndex, to: firstIndex)
    print("index: ", index)   //index: 2
}
else {
    print("symbol not found")
}

7

在Swift 4中:

获取字符串中字符的索引:

let str = "abcdefghabcd"
if let index = str.index(of: "b") {
   print(index) // Index(_compoundOffset: 4, _cache: Swift.String.Index._Cache.character(1))
}

使用Swift 4从String创建SubString(前缀和后缀):

let str : String = "ilike"
for i in 0...str.count {
    let index = str.index(str.startIndex, offsetBy: i) // String.Index
    let prefix = str[..<index] // String.SubSequence
    let suffix = str[index...] // String.SubSequence
    print("prefix \(prefix), suffix : \(suffix)")
}

输出量

prefix , suffix : ilike
prefix i, suffix : like
prefix il, suffix : ike
prefix ili, suffix : ke
prefix ilik, suffix : e
prefix ilike, suffix : 

如果要在两个索引之间生成子字符串,请使用:

let substring1 = string[startIndex...endIndex] // including endIndex
let subString2 = string[startIndex..<endIndex] // excluding endIndex

是什么_compoundOffset,字节串,直到这一点在多少?
ArielSD

6

在Swift中可以执行此操作,但是它需要花费更多的行,这是一个indexOf()可以完成预期功能的函数:

func indexOf(source: String, substring: String) -> Int? {
    let maxIndex = source.characters.count - substring.characters.count
    for index in 0...maxIndex {
        let rangeSubstring = source.startIndex.advancedBy(index)..<source.startIndex.advancedBy(index + substring.characters.count)
        if source.substringWithRange(rangeSubstring) == substring {
            return index
        }
    }
    return nil
}

var str = "abcde"
if let indexOfCD = indexOf(str, substring: "cd") {
    let distance = str.startIndex.advancedBy(indexOfCD)
    print(str.substringToIndex(distance)) // Returns "ab"
}

此功能尚未优化,但可以处理短字符串。


7
非常令人沮丧的是他们还没有将其添加到Swift库中!
莫里·马科维兹

extension String在utils.swift类中添加了一个对其他所有人都将可用的类
StephenBoesch

btw以上似乎是O(N^2)在字符串的长度..?
StephenBoesch

2

在Swift版本3中,String没有类似的功能-

str.index(of: String)

如果子字符串需要索引,则获取范围的方法之一是。我们在返回范围的字符串中包含以下函数-

str.range(of: <String>)
str.rangeOfCharacter(from: <CharacterSet>)
str.range(of: <String>, options: <String.CompareOptions>, range: <Range<String.Index>?>, locale: <Locale?>)

例如,在str中查找游戏的首次出现的索引

var str = "play play play"
var range = str.range(of: "play")
range?.lowerBound //Result : 0
range?.upperBound //Result : 4

注意:范围是可选的。如果找不到字符串,它将为零。例如

var str = "play play play"
var range = str.range(of: "zoo") //Result : nil
range?.lowerBound //Result : nil
range?.upperBound //Result : nil

2

这里有三个密切相关的问题:

  • 在Cocoa NSString世界(基础)中,所有子字符串查找方法均已结束。

  • Foundation NSRange与Swift Range不匹配;前者使用开始和长度,后者使用端点

  • 通常,Swift字符是使用String.Index而不是Int进行索引的,而Foundation字符使用Int进行索引的,并且它们之间没有简单的直接转换(因为Foundation和Swift对组成字符的想法不同)

考虑到所有这些,让我们考虑如何编写:

func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
    // ?
}

s2必须s使用String Foundation方法来查找子字符串。结果范围返回给我们,而不是作为NSRange(即使这是一个Foundation方法),而是作为Range String.Index(包裹在Optional中,以防根本找不到子字符串)。但是,其他号码,fromInt。因此,我们无法形成涉及两者的任何范围。

但是我们不必!我们要做的就是使用采用的方法将原始字符串的末尾切开String.Index,并采用采用Int的方法将原始字符串的起始切下。幸运的是,存在这样的方法!像这样:

func substring(of s: String, from:Int, toSubstring s2 : String) -> Substring? {
    guard let r = s.range(of:s2) else {return nil}
    var s = s.prefix(upTo:r.lowerBound)
    s = s.dropFirst(from)
    return s
}

或者,如果您希望能够将此方法直接应用于字符串,例如...

let output = "abcde".substring(from:0, toSubstring:"cd")

...然后将其扩展为String:

extension String {
    func substring(from:Int, toSubstring s2 : String) -> Substring? {
        guard let r = self.range(of:s2) else {return nil}
        var s = self.prefix(upTo:r.lowerBound)
        s = s.dropFirst(from)
        return s
    }
}

这是在复制原始字符串吗?如果原始字符串很长而又重复了操作怎么办?可以使用jvm世界中的零数据复制来完成。
StephenBoesch

@javadba不能在派生子字符串时进行复制,这就是子字符串的重点。基本上,该代码只是走了一堆指针。
马特

好的-我看到了dropFirst,还没有看过它是如何实现的。我们如何才能将最终收益提取Substring为a String?我看到超级冗长的帖子只是对..
StephenBoesch

只是强制使用字符串。我不确定当时是否有副本;只要没有修改此字符串或原始字符串,就可能没有,但是我不清楚String如何采用写时复制的细节。
马特

好的-强迫我们走了。这样做时,我收到警告“从子字符串到字符串的转换始终失败”as! String
StephenBoesch

2

Leo Dabus的答案很好。这是我基于他的回答compactMap而避免Index out of range错误的回答。

迅捷5.1

extension StringProtocol {
    func ranges(of targetString: Self, options: String.CompareOptions = [], locale: Locale? = nil) -> [Range<String.Index>] {

        let result: [Range<String.Index>] = self.indices.compactMap { startIndex in
            let targetStringEndIndex = index(startIndex, offsetBy: targetString.count, limitedBy: endIndex) ?? endIndex
            return range(of: targetString, options: options, range: startIndex..<targetStringEndIndex, locale: locale)
        }
        return result
    }
}

// Usage
let str = "Hello, playground, playground, playground"
let ranges = str.ranges(of: "play")
ranges.forEach {
    print("[\($0.lowerBound.utf16Offset(in: str)), \($0.upperBound.utf16Offset(in: str))]")
}

// result - [7, 11], [19, 23], [31, 35]


1

迅捷5

    extension String {
    enum SearchDirection {
        case first, last
    }
    func characterIndex(of character: Character, direction: String.SearchDirection) -> Int? {
        let fn = direction == .first ? firstIndex : lastIndex
        if let stringIndex: String.Index = fn(character) {
            let index: Int = distance(from: startIndex, to: stringIndex)
            return index
        }  else {
            return nil
        }
    }
}

测试:

 func testFirstIndex() {
        let res = ".".characterIndex(of: ".", direction: .first)
        XCTAssert(res == 0)
    }
    func testFirstIndex1() {
        let res = "12345678900.".characterIndex(of: "0", direction: .first)
        XCTAssert(res == 9)
    }
    func testFirstIndex2() {
        let res = ".".characterIndex(of: ".", direction: .last)
        XCTAssert(res == 0)
    }
    func testFirstIndex3() {
        let res = "12345678900.".characterIndex(of: "0", direction: .last)
        XCTAssert(res == 10)
    }

0

迅捷5

   let alphabat = "abcdefghijklmnopqrstuvwxyz"

    var index: Int = 0
    
    if let range: Range<String.Index> = alphabat.range(of: "c") {
         index = alphabat.distance(from: alphabat.startIndex, to: range.lowerBound)
        print("index: ", index) //index: 2
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.