How can I remove all white space from the beginning and end of a string?
Like so:
"Test User Image"
returns "Test User Image"
"Test User Image "
returns "Test User Image"
" Test User Image "
returns "Test User Image"
" Test User Image "
returns "Test User Image"
Question: How to remove all the white spaces from a string at the beginning or end?
Can someone please explain to me how to remove all the white spaces, I’ve tried with the below answers but no results yet.
How should I remove all the leading spaces from a string? – swift
How to remove all the spaces and nr in a String?
Any help would be greatly appreciated.
Thanks in advance.
3
Answers
You can use this:
Please check it out,
I hope you got the answer.
You can use
name.trimmingCharacters(in:)
using the WhiteSpace character set as parameter, but this has a cost because it calls the underlyingNSString
function.A pure Swift implementation may look as below:
Prints: "
Test User Image
"