-1

For example, I have the following string:

e it z should a make q sense

and I want it to be:

it should make sense

Is there a single way to do this without having to create or loop through some character set?

Ethan Allen
  • 13,099
  • 22
  • 89
  • 175
  • It looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Nov 30 '20 at 10:00

1 Answers1

2

You can use a regex to achieve your goals.

let stringWithSingleChars = "e it z should a make q sense"
stringWithSingleChars.replacingOccurrences(of: #"\b\w\b"#, with: "", options: .regularExpression, range: nil)

The pattern matches a word boundary, then a single word character, then a whitespace, resulting in all single-characters followed by a space being matched.

Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80