Does Swift have a trim method for strings like Java or C#?
Hello everyone! I just started learning Swift after Java, and I ran into a simple task: I need to remove extra spaces and newline characters from the beginning and end of a string.\n\nIn Java I would write `str.trim()`, in C# — `str.Trim()`. But in Swift I can't find anything similar. I tried writing this:\n\n```swift\nlet text = " Hello, world! "\nlet trimmed = text.trim() // Error: Value of type 'String' has no member 'trim'\n```\n\nI get a compilation error. I searched the documentation — I only found `trimmingCharacters(in:)`, but it takes some `CharacterSet`, and I don't understand how to use it correctly.\n\nI just need to remove spaces and line breaks from both ends of the string, like it's done in most languages. Do I really need to write my own function for this in Swift? Please tell me how to
No answers yet.
Log in to leave an answer