Swift : How to get the string before a certain character?

前端 未结 11 1877
北恋
北恋 2020-12-24 01:45

How do I get the string before a certain character in swift? The code below is how I did it in Objective C, but can\'t seem to perform the same task in Swift. Any tips or su

相关标签:
11条回答
  • 2020-12-24 02:31

    You can use rangeOfString, but it returns a Range<String.Index> type, not a NSRange:

    let string = "hello Swift"
    if let range = string.rangeOfString("Swift") {
        print(string.substringToIndex(range.startIndex))
    }
    
    0 讨论(0)
  • 2020-12-24 02:36
    let string = "Hello-world"
    if let range = string.range(of: "-") {
    let firstPart = string[(string.startIndex)..<range.lowerBound]
     print(firstPart) 
    }
    

    Output is: Hello

    0 讨论(0)
  • 2020-12-24 02:36

    Below is kind of a whole combo

    let string = "This a string split using * and this is left."
    if let range = string.range(of: "*") {
        let lastPartIncludingDelimiter = string.substring(from: range.lowerBound)
        print(lastPartIncludingDelimiter) // print * and this is left.
    
        let lastPartExcludingDelimiter = string.substring(from: range.upperBound)
        print(lastPartExcludingDelimiter) // print and this is left.
    
        let firstPartIncludingDelimiter = string.substring(to: range.upperBound)
        print(firstPartIncludingDelimiter) // print This a string split using *
    
        let firstPartExcludingDelimiter = string.substring(to: range.lowerBound)
        print(firstPartExcludingDelimiter) // print This a string split using
    }
    
    0 讨论(0)
  • 2020-12-24 02:37

    You can do the same with rangeOfString() provided by String class

    let string = "Hello Swift"
    if let range = string.rangeOfString("Swift") {
       let firstPart = string[string.startIndex..<range.startIndex]
       print(firstPart) // print Hello
    }
    

    You can also achieve it with your method substringToIndex()

    let string = "Hello Swift"
    if let range = string.rangeOfString("Swift") {
       firstPart = string.substringToIndex(range.startIndex)
       print(firstPart) // print Hello
    }
    

    Swift 3 UPDATE:

    let string = "Hello Swift"
    if let range = string.range(of: "Swift") {
        let firstPart = string[string.startIndex..<range.lowerBound]
        print(firstPart) // print Hello
    }
    

    Hope this can help you ;)

    0 讨论(0)
  • 2020-12-24 02:39

    If you want a solution that doesn't involve pulling in foundation, you can do it with find and slicing:

    let str = "Hello, I must be going."
    
    if let comma = find(str, ",") {
        let substr = str[str.startIndex..<comma]
        // substr will be "Hello"
    }
    

    If you explicitly want an empty string in the case where no such character is found, you can use the nil-coalescing operator:

    let str = "no comma"
    let comma = find(str, ",") ?? str.startIndex
    let substr = str[str.startIndex..<comma]  // substr will be ""
    

    Note, unlike the componentsSeparatedByString technique, this does not require the creation of an array, and only requires scanning up to the first occurrence of the character rather than breaking the entire string up into the character-delimited array.

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