Swift version of componentsSeparatedByString

后端 未结 4 669
攒了一身酷
攒了一身酷 2020-11-29 08:04

I know its noob question, i really search around before ask. But there is not exact answer of what i want to know. How we split string into the array without using Objectiv

相关标签:
4条回答
  • 2020-11-29 08:38

    If you want to split a string by a given character then you can use the built-in split() method, without needing Foundation:

    let str = "Today is so hot"
    let arr = split(str, { $0 == " "}, maxSplit: Int.max, allowEmptySlices: false)
    println(arr) // [Today, is, so, hot]
    

    Update for Swift 1.2: The order of the parameters changed with Swift 1.2 (Xcode 6.3), compare split now complains about missing "isSeparator":

    let str = "Today is so hot"
    let arr = split(str, maxSplit: Int.max, allowEmptySlices: false, isSeparator: { $0 == " "} )
    println(arr) // [Today, is, so, hot]
    

    Update for Swift 2: See Stuart's answer.

    Update for Swift 3:

    let str = "Today is so hot"
    let arr = str.characters.split(separator: " ").map(String.init)
    print(arr)
    
    0 讨论(0)
  • 2020-11-29 08:40

    In Swift 3.0 Use components(separatedBy:) on String than componentsSeparatedByString.

    Sharing code sample

    let tempString = "1.Wash your hands\n2.Check you phone\n3.Click home button".components(separatedBy: "\n")
    print(tempString)
    
    0 讨论(0)
  • 2020-11-29 08:50

    "I know it doesnt work"

    Well, for me, it does. In the Swift REPL:

      1> import Foundation
      2> "a b c".componentsSeparatedByString(" ")
    $R6: String[] = size=3 {
      [0] = {
        core = {
          _baseAddress = Builtin.RawPointer = 0x0000000100407980
          _countAndFlags = -4611686018427387903
          _owner = Some {
            Some = @"a"
          }
        }
      }
      [1] = {
        core = {
          _baseAddress = Builtin.RawPointer = 0x0000000100408e50 -> 0x00007fff7cde0062 (void *)0x001b00007fff7cdd
          _countAndFlags = -4611686018427387903
          _owner = Some {
            Some = @"b"
          }
        }
      }
      [2] = {
        core = {
          _baseAddress = Builtin.RawPointer = 0x0000000100408dd0
          _countAndFlags = -4611686018427387903
          _owner = Some {
            Some = @"c"
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-29 09:01

    In Swift 2, the syntax for this has got more concise. The top-level split function is now a method on CollectionType (which String.CharacterView conforms to).

    There are two versions of the method, the first takes a closure as a predicate to indicate whether a given element should be treated as a separator, and the other simply takes an element to specify as the separator - this is likely what you want for 99% of string splits. They both have a couple of defaulted parameters (see the docs for details) which helps keep the method call nice and clear.

    It's worth noting that split(_:) returns a SubSequence (basically a Slice), so in most cases needs transforming back into an array of Strings which is generally more useful.

    let str = "Today is so hot"
    let words = str.characters.split(" ").map { String($0) }  // or `.map(String.init)`
    

     Explaining the shorthand initializer expression (map(String.init))

    map is a method that accepts a function as an argument. Most of the time, you probably just use a trailing closure (an unnamed function) as the argument to map, e.g.:

    func createString(from character: Character) -> String {
        // ...
    }
    
    let strings = characters.map { createString(from: $0) }
    

    But the following is more concise, and just as valid:

    let strings = characters.map(createString(from:))
    // or even:
    let strings = characters.map(createString) // argument names inferred from context.
    

    In the above example, createString(from:) and createString are both function expressions. map expects a function as an argument, and a function expression can be passed as that argument. You can pass a function expression anywhere that accepts a function as an argument, which is why you can use sort like this for a types that conform to Comparable:

    let sortedStrings = strings.sorted(by: <)
    
    // `<` is just a function expression for a function that is essentially declared something like this:
    func <(lhs: String, rhs: String) -> Bool {
        // ...
    }
    

    String.init is an initializer expression. It can be used in just the same way as a function expression – it is itself describing a function that takes a single Character as an argument, and returns a value of type String (though String.init is overloaded and can be passed many different types of argument).

    To help clarify what is going on, consider the following code, where each invocation of map does exactly the same thing, but using an increasingly concise syntax:

    // - Closure expression syntax used.
    // - Explicit argument names & types.
    // - Long-hand string initialization.
    let strings = characters.map({ (character: CharacterView) -> String in
        return String.init(character)
    })
    
    // - Trailing closure syntax used (parentheses omitted).
    // - Closure argument and return types inferred from context (type names omitted).
    // - Short-hand (standard, really) string initialization.
    let strings = characters.map { character in
        return String(character)
    }
    
    // - Implicit return since using single expression in the closure (`return` keyword omitted).
    let strings = characters.map { character in String(character) }
    
    // - Shorthand argument names (`in` keyword and argument names omitted).
    let strings = characters.map { String($0) }
    
    // - Initializer expression syntax used (curly braces omitted, argument implicitly passed to the initializer).
    let strings = characters.map(String.init(_:))
    
    // - Initializer expression argument names inferred from context. 
    let strings = characters.map(String.init)
    
    0 讨论(0)
提交回复
热议问题