Skip to main content
edited title
Link

Swift - iOS: Remove spaces inside a String before or after any special character in given string

Source Link

Swift - iOS: Remove spaces before or after any special character in given string

I have to do validation to check user entered answer to an application. I want to remove spaces (if any) left or before for bellow special characters.

  • /
  • ,
  • :
  • ;
  • -
  • .

So the final output should be like this.

Ex:

Correct answer => a/b

Answers need to accept => ' a/b ', 'a/ b', 'a /b', 'a/ b ', 'a/b '

I can do this using replacingOccurrences function by replacing all possible values. Is there any better solution for this?