简体   繁体   中英

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.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM