Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

component(separatedBy:) versus .split(separator: )

Tags:

swift

In Swift 4, new method .split(separator:) is introduced by apple in String struct. So to split a string with whitespace which is faster for e.g..

let str = "My name is Sudhir"

str.components(separatedBy: " ")

//or 

str.split(separator: " ")
like image 248
Sudhir kumar Avatar asked Sep 21 '17 13:09

Sudhir kumar


People also ask

How do you split a string into two parts in Swift?

Swift String split() The split() method breaks up a string at the specified separator and returns an array of strings.

How do you split a string in Objective C?

Objective-C Language NSString Splitting If you need to split on a set of several different delimiters, use -[NSString componentsSeparatedByCharactersInSet:] . If you need to break a string into its individual characters, loop over the length of the string and convert each character into a new string.

How to split string into words Swift?

To split a string to an array in Swift by a character, use the String. split(separator:) function. However, this requires that the separator is a singular character, not a string.

What is components in Swift?

The components(separatedBy:) method is used to divide a string into substrings using the specified string separator.


2 Answers

Performance aside, there is an important difference between split(separator:) and components(separatedBy:) in how they treat empty subsequences.

They will produce different results if your input contains a trailing whitespace:

    let str = "My name is Sudhir " // trailing space

    str.split(separator: " ")
    // ["My", "name", "is", "Sudhir"]

    str.components(separatedBy: " ")
    // ["My", "name", "is", "Sudhir", ""] ← Additional empty string

To have both produce the same result, use the omittingEmptySubsequences:false argument (which defaults to true):

    // To get the same behavior:
    str.split(separator: " ", omittingEmptySubsequences: false)
    // ["My", "name", "is", "Sudhir", ""]

Details here:

https://developer.apple.com/documentation/swift/string/2894564-split

like image 126
Mark Avatar answered Oct 22 '22 20:10

Mark


I have made sample test with following Code.

  var str = """
                One of those refinements is to the String API, which has been made a lot easier to use (while also gaining power) in Swift 4. In past versions of Swift, the String API was often brought up as an example of how Swift sometimes goes too far in favoring correctness over ease of use, with its cumbersome way of handling characters and substrings. This week, let’s take a look at how it is to work with strings in Swift 4, and how we can take advantage of the new, improved API in various situations. Sometimes we have longer, static strings in our apps or scripts that span multiple lines. Before Swift 4, we had to do something like inline \n across the string, add an appendOnNewLine() method through an extension on String or - in the case of scripting - make multiple print() calls to add newlines to a long output. For example, here is how TestDrive’s printHelp() function (which is used to print usage instructions for the script) looks like in Swift 3  One of those refinements is to the String API, which has been made a lot easier to use (while also gaining power) in Swift 4. In past versions of Swift, the String API was often brought up as an example of how Swift sometimes goes too far in favoring correctness over ease of use, with its cumbersome way of handling characters and substrings. This week, let’s take a look at how it is to work with strings in Swift 4, and how we can take advantage of the new, improved API in various situations. Sometimes we have longer, static strings in our apps or scripts that span multiple lines. Before Swift 4, we had to do something like inline \n across the string, add an appendOnNewLine() method through an extension on String or - in the case of scripting - make multiple print() calls to add newlines to a long output. For example, here is how TestDrive’s printHelp() function (which is used to print usage instructions for the script) looks like in Swift 3
          """

    var newString = String()

    for _ in 1..<9999 {
        newString.append(str)
    }

    var methodStart = Date()

 _  = newString.components(separatedBy: " ")
    print("Execution time Separated By: \(Date().timeIntervalSince(methodStart))")

    methodStart = Date()
    _ = newString.split(separator: " ")
    print("Execution time Split By: \(Date().timeIntervalSince(methodStart))")

I run above code on iPhone6 , Here are the results

Execution time Separated By: 8.27463299036026 Execution time Split By: 4.06880903244019

Conclusion : split(separator:) is faster than components(separatedBy:).

like image 43
Sudhir kumar Avatar answered Oct 22 '22 21:10

Sudhir kumar