0

I referred this SO post to remove whitespaces and newline characters from a string. But in my string, I may have extra whitespaces as well as extra newline characters. I want to remove the unnecessary \n's and whitespaces from that string.

But if there is a string like so..."This \n is a st\tri\rng" then I don't want Thisisastring as the result but instead something like this..

This is a string

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • There is no well defined logic here. Hence you cannot code for this. – Rakesha Shastri Apr 04 '19 at 05:26
  • 1
    Possible duplicate of https://stackoverflow.com/questions/33058676/how-to-remove-multiple-spaces-in-strings-with-swift-2. – Martin R Apr 04 '19 at 05:27
  • Would suggest you regex format to determine extra spacing between words and remove them. – Sharkes Monken Apr 04 '19 at 05:27
  • Try `string.trimmingCharacters(in: .whitespacesAndNewlines)` – Kamran Apr 04 '19 at 05:29
  • 1
    @Kamran That only removes leading and trailing whitespace. – rmaddy Apr 04 '19 at 05:30
  • 1
    Its easy to replace contiguous whitespaces with one whitespace, by regular expression or split-join. However, that `st\tri\rng` thing is completely not predictable. How can you differentiate whitespaces within a word and between words? – Ricky Mo Apr 04 '19 at 05:32
  • let newStr = urSTring.replacingOccurrences(of: " ", with: "") – Siddhant Nigam Apr 04 '19 at 05:42
  • Possible duplicate of [How to remove all the spaces and \n\r in a String?](https://stackoverflow.com/questions/34940044/how-to-remove-all-the-spaces-and-n-r-in-a-string) – Kamran Apr 04 '19 at 05:44
  • Unless you provide rules to differentiate spaces within a word and spaces betweens words, it is not possible. – Ricky Mo Apr 04 '19 at 05:50
  • Ok...so if my string is `This \n\n is a string` i.e. having just extra newline characters and whitespaces, then how can I get a something like `This is a string` –  Apr 04 '19 at 05:51

1 Answers1

0

To replace contiguous spaces with a single space, replace Regular Expression \s+ with a single space:

let str = "This \n\n is a string"
if let regex = try? NSRegularExpression(pattern: "\\s+", options: NSRegularExpression.Options.caseInsensitive)
{
    let result = regex.stringByReplacingMatches(in: str, options: [], range: NSMakeRange(0, str.count), withTemplate: " ")
    print(result)  //output: "This is a string"
}
Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
  • Thanks @Ricky Mo...But there's one more concern..if I have a text one below the other, then with the above code, I'll finally get it one after the other as it deletes the newlines. What has to be done if I want the text to appear one below the other itself but without the `\n` being shown..? –  Apr 04 '19 at 06:27
  • By `This \n\n is a string` -> `This is a string`, you just meant you want to remove the newlines. If you want to preserve some newlines, what is your rule to determine which newlines to be preserved and which to be removed? – Ricky Mo Apr 04 '19 at 07:23