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?

sameera
  • 23
  • 5

3 Answers3

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
Quoc Nguyen
  • 2,839
  • 6
  • 23
  • 28
  • 1
    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 Mar 22 '19 at 05:46
  • @sameera Use `"\\."` instead of `"."` – ielyamani Mar 22 '19 at 05:46
  • @sameera Sorry about it, updated answer with suggestion from ielyamani – Quoc Nguyen Mar 22 '19 at 08:58
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.

Amir Khan
  • 1,318
  • 1
  • 14
  • 39
  • 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" – sameera Mar 22 '19 at 05:41
  • You need to read the question again since you have clearly misunderstood it. Not all spaces should be removed, please improve your answer. – Joakim Danielson Mar 22 '19 at 06:53
  • My bad I got that wrong. @JoakimDanielson I've updated my answer. Please check. – Amir Khan Mar 22 '19 at 08:53
  • 2
    This answer is more optimised than accepted answer. Cheers! – Manish Mar 22 '19 at 11:14
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
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40