Index of a substring in a string with Swift

前端 未结 11 1551
自闭症患者
自闭症患者 2020-11-22 14:24

I\'m used to do this in JavaScript:

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

Swift doesn\'t have t

相关标签:
11条回答
  • 2020-11-22 14:44

    In Swift 4 :

    Getting Index of a character in a string :

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

    Creating SubString (prefix and suffix) from String using Swift 4:

    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)")
    }
    

    Output

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

    If you want to generate a substring between 2 indices , use :

    let substring1 = string[startIndex...endIndex] // including endIndex
    let subString2 = string[startIndex..<endIndex] // excluding endIndex
    
    0 讨论(0)
  • 2020-11-22 14:47

    In the Swift version 3, String doesn't have functions like -

    str.index(of: String)
    

    If the index is required for a substring, one of the ways to is to get the range. We have the following functions in the string which returns range -

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

    For example to find the indexes of first occurrence of play in str

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

    Note : range is an optional. If it is not able to find the String it will make it nil. For example

    var str = "play play play"
    var range = str.range(of: "zoo") //Result : nil
    range?.lowerBound //Result : nil
    range?.upperBound //Result : nil
    
    0 讨论(0)
  • 2020-11-22 14:47

    Have you considered using NSRange?

    if let range = mainString.range(of: mySubString) {
      //...
    }
    
    0 讨论(0)
  • 2020-11-22 14:50

    Using String[Range<String.Index>] subscript you can get the sub string. You need starting index and last index to create the range and you can do it as below

    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")
    }
    

    If you don't define the start index this operator ..< , it take the starting index. You can also use str[str.startIndex..<range.lowerBound] instead of str[..<range.lowerBound]

    0 讨论(0)
  • 2020-11-22 14:50

    Swift 5

    Find index of substring

    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")
    }
    

    Find index of Character

    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")
    }
    
    0 讨论(0)
  • 2020-11-22 14:56

    Doing this in Swift is possible but it takes more lines, here is a function indexOf() doing what is expected:

    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"
    }
    

    This function is not optimized but it does the job for short strings.

    0 讨论(0)
提交回复
热议问题