0

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?

6
  • can you show some sample inputs Commented Mar 22, 2019 at 4:26
  • Ex: ' apple / orange', 'apple /orange ', 'apple / orange ', ' apple / orange', => These values need to convert to 'apple/orange' Commented Mar 22, 2019 at 4:30
  • Possible duplicate of How to remove whitespaces in strings in Swift? Commented Mar 22, 2019 at 4:38
  • show your tried code Commented Mar 22, 2019 at 4:39
  • @RakeshaShastri I don't want to trim the string. I need to remove spaces on or before above mentioned special characters (Inside a string). Commented Mar 22, 2019 at 4:41

3 Answers 3

2

You can using regular expression for replace the string with format [ ]+{special_char} and {special_char}[ ]+.

Edit

Update "." to "\\."

Thanks ielyamani

For example

func acceptedAnswer(of answer: String) -> String { let specialChars = ["/", ":", ",", ";", "-", "\\."] var newAnswer = answer.trimmingCharacters(in: .whitespacesAndNewlines) for specialChar in specialChars { let beforeCharRegex = "[ ]+" + specialChar let afterCharRegex = specialChar + "[ ]+" newAnswer = newAnswer.replacingOccurrences(of: beforeCharRegex, with: specialChar, options: .regularExpression, range: nil) newAnswer = newAnswer.replacingOccurrences(of: afterCharRegex, with: specialChar, options: .regularExpression, range: nil) } return newAnswer } print(acceptedAnswer(of: " apple / orange : banana ")) // apple/orange:banana 
Sign up to request clarification or add additional context in comments.

3 Comments

I checked your answer. But if there any space between words this solution will not work. Ex: " Apple / Green orange" => Answer should be "Apple/Green orange" - But in your solution answer will be "Apple/Orance.anana"
@sameera Use "\\." instead of "."
@sameera Sorry about it, updated answer with suggestion from ielyamani
0

You can use replacingOccurrences which would replace your symbol with whitespaces with just this symbol. For this purpose you can use recursive method

func removed(in text: String) -> String { let symbols = ["/", ",", ":", ";", "-", "."] var newText = text symbols.forEach { newText = replaced(in: newText, for: $0) } return newText } func replaced(in text: String, for symbol: String) -> String { var newText = text let left = " \(symbol)" newText = newText.replacingOccurrences(of: left, with: symbol) let right = "\(symbol) " newText = newText.replacingOccurrences(of: right, with: symbol) return newText != text ? replaced(in: newText, for: symbol) : newText } 

Usage:

let string = "Apple / Orange Swift . ObjC Moon : Sun USA - UK" print(removed(in: string)) 
Apple/Orange Swift.ObjC Moon:Sun USA-UK 

Comments

0

Remove before and after space of Special character using Range -

 let specialChars = ["/",":", ",", ";", "-", "."] let aString = " AB / CD EF ; G : H , MN O - P" var trimmedString = aString.trimmingCharacters(in: .whitespacesAndNewlines) for specialChar in specialChars { if let beforeSpacerange = trimmedString.range(of: " \(specialChar)") { trimmedString.replaceSubrange(beforeSpacerange, with: specialChar) } if let afterSpacerange = trimmedString.range(of: "\(specialChar) ") { trimmedString.replaceSubrange(afterSpacerange, with: specialChar) } } debugPrint(trimmedString) 

Hope it will help you. Let me know if you are still having any issue.

Happy coding.

4 Comments

I checked your answer, But I don't need to replace all spaces inside the string. I need to replace spaces which are after or before above special characters. Ex: " Apple / Orance Banana" => "Apple/Orance Banana"
You need to read the question again since you have clearly misunderstood it. Not all spaces should be removed, please improve your answer.
My bad I got that wrong. @JoakimDanielson I've updated my answer. Please check.
This answer is more optimised than accepted answer. Cheers!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.