Is there a trim method in Swift for trimming whitespace from a string?
Hi everyone! I just started learning Swift and ran into a problem: I need to remove extra spaces and newline characters at the beginning and end of a string. In other languages, like Java or JavaScript, there is a `trim()` method that does this with one command. But I haven't found one in Swift.
Here's an example of what I need to do:
```swift
let userInput = " Hello, world! \n"
// I want to get "Hello, world!" without spaces and newlines
```
I tried writing something like `userInput.trim()`, but Xcode gives an error "Value of type 'String' has no member 'trim'". I tried googling, but found many different options: some people write about `trimmingCharacters`, others about `components(separatedBy:)` — I'm already confused.
Please tell me, is there a built-in way to trim a string in Swift? And
No answers yet.
Log in to leave an answer