5

I have a String Array where I have need to trim the Spaces in each string.. Please let me know for any simple way without looping them in for loop.

Ex: ["Apple ", "Bike ", " Cat", " Dog "] I have an Array like this. I want all these spaces to be trimmed.

6
  • what options have you considered? why not a simple for loop if that's an approach you know? Commented Jun 17, 2016 at 11:03
  • I donot want to loop it as its affecting the performance. As we do have many options in Swift along with Set Properties, like filter, enumerate kind of options, I am looking a feature kind of this Commented Jun 17, 2016 at 11:04
  • 1
    have you profiled? and how many items are in your array? whatever you do will involve a loop even if you don't explicitly write it... Commented Jun 17, 2016 at 11:04
  • Affecting the performance? How do you know it is affecting the performance? The only thing I can think of is something is returning nil when you're accessing this array. I'm I right? Commented Jun 17, 2016 at 11:06
  • 1
    @vacawama's use of the map statement is certainly more "Swift-like" than using a for loop, but I bet the performance is all but identical. There is no magic here. If you need to check all the strings in an array and trim leading/trailing spaces then you need to iterate through all the strings. That is what the map statement does internally. It just uses functional rather than imperative syntax. Commented Jun 17, 2016 at 11:37

3 Answers 3

14

This is a good application for map. Use it to take each item in your array and apply trimmingCharacters(in:) to remove the whitespace from the ends of your strings.

import Foundation // or UIKit or Cocoa

let array =  ["Apple ", "Bike ", " Cat", " Dog "]
let trimmed = array.map { $0.trimmingCharacters(in: .whitespaces) }

print(trimmed)  // ["Apple", "Bike", "Cat", "Dog"]

@DuncanC's comment is an important point so I'm highlighting it here:

@vacawama's use of the map statement is certainly more "Swift-like" than using a for loop, but I bet the performance is all but identical. There is no magic here. If you need to check all the strings in an array and trim leading/trailing spaces then you need to iterate through all the strings. That is what the map statement does internally. It just uses functional rather than imperative syntax.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer.. I am looking for it only.
Also I would find another method. I am posting in as another answer here.
This is probably the fastest and most essential solution you can get.
2

Swift 3-4 Compatible

(Based on @vacawama's answer)

let items =  ["Apple ", "Bike ", " Cat", " Dog "]
let trimmed = items.map({
  return $0.trimmingCharacters(in: .whitespaces)
})
print(trimmed)  // ["Apple", "Bike", "Cat", "Dog"]

Comments

-2

We can Join the array as a String, and can be replaced the space .

Like,

 let array =  ["Apple ", "Bike ", " Cat", " Dog "]
 var jointString = array.joinWithSeparator(",")
 jointString = jointString.stringByReplacingOccurrencesOfString(" ", withString: "")

 let newArray = jointString.componentsSeparatedByString(",")
 print(newArray)

1 Comment

This is less elegant, and most importantly, performance-wise, much less efficient than @vacawama's answer... I don't understand why you would prefer this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.