寻找一种在小写和大写字母之间放置空格的方法。 示例代码:
var exampleString = "SavedAs"
var exampleString2 = "ExampleTwo"
finalString =“另存为” FinalString2 = "示例二"
这是我尝试过的方法,但不起作用:
extension String {
func stringByAddingSpaceaA(subStrings: [String]) -> String {
var resultString = self
subStrings.map { resultString = resultString.replacingOccurrences(of: $0, with: "/([a-z]) ([A-Z])/") }
return resultString
}
}
let chars3: [String] = ["/([a-z])([A-Z])/"]
var exampleString = "SavedAs"
let finalString = exampleString.stringByAddingSpaceaA(subStrings: chars3)
您可以将
replacing
与简单的正则表达式一起使用,该正则表达式可以查找任何大写字母,然后修剪任何多余的空格
let output = exampleString
.replacing(/(?=[A-Z])/, with: " ")
.trimmingCharacters(in: .whitespaces)