Prashant Pukale
Prashant Pukale

Reputation: 11

Swift string comparison failure due to newlines

I am trying to compare two strings 1. String read from a .strings file using the api, String(contentsOf: localizableFilePath, encoding: .ascii).propertyListFromStringsFileFormat() 2. Strings to be written to the strings file

The string comparison fails when there are newlines in the strings, i,e

the string 1 has newLine character in them so its like "something something"

and string 2 is like "something \nsomething"

and the comparison fails because of this.

Upvotes: 0

Views: 278

Answers (1)

Abizern
Abizern

Reputation: 150615

You can try replacing occurrences of newline characters with an empty string: For example:

let inputString = "Something \nSomething"
let test = "Something Something"

test == inputString.replacingOccurrences(of: "\n", with: "") // true

enter image description here

Upvotes: 1

Related Questions