由于换行而导致的快速字符串比较失败

问题描述 投票:0回答:1

我正在尝试比较两个字符串1.使用api String(contentsOf: localizableFilePath, encoding: .ascii).propertyListFromStringsFileFormat()从.strings文件读取的字符串2.要写入字符串文件的字符串

当字符串中有换行符时,字符串比较失败,即,e

字符串1中包含newLine字符,因此它就像"something something"

并且字符串2类似于"something \nsomething"

并且比较因此失败。

swift string nslocalizedstring
1个回答
1
投票
您可以尝试用空字符串替换出现的换行符:例如:

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

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.