用不同的文本长度替换字符串中不同长度的数字
Replace numeric of different length in a string with different text length
问题:数字的长度可能是 1、200、1000、39 99995 等,需要用文本替换,例如。 "Apple" 与数值的长度比较不同。
let str: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
预期结果="hello i have Apple object of Apple string class with Apple object, similar to Apple errors"
我试过以下代码:
var originalString: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
let strippedString: NSMutableString = NSMutableString(capacity: originalString.characters.count)
var numArray: [String] = []
var locArray: [NSNumber] = []
var scanner: NSScanner = NSScanner(string:originalString)
let numbers: NSCharacterSet = NSCharacterSet(charactersInString: "0123456789")
while scanner.atEnd == false {
var buffer: NSString?
if scanner.scanCharactersFromSet(numbers, intoString: &buffer) {
strippedString.appendString(buffer! as String)
numArray.append(buffer! as String)
locArray.append(scanner.scanLocation)
}
else {
scanner.scanLocation = (scanner.scanLocation + 1)
}
}
for (index, _) in numArray.enumerate() {
var loc : Int = Int(locArray[index] ) - (String(numArray[index]).characters.count)
let len = String(numArray[index]).characters.count
let dupStr = "Apple"
if(index != 0 && len != dupStr.characters.count)
{
loc = loc + (dupStr.characters.count - len) + 1
}
originalString.replaceRange(originalString.startIndex.advancedBy(loc)..<originalString.startIndex.advancedBy(loc + len), with: dupStr)
}
print(originalString)
Swift 2
NSScanner 很棒,但是如果您想要一个更简单的解决方案来完成此任务,您可以使用 componentsSeparatedByString
、map
、Int()
和 joinWithSeparator
,如下所示:
let originalString = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
let tokens = originalString.componentsSeparatedByString(" ")
let newTokens = tokens.map { (token) -> String in
if let _ = Int(token) {
return "Apple"
}
return token
}
let result = newTokens.joinWithSeparator(" ")
print(result)
打印:
hello i have Apple object of Apple string class with Apple object, similar to Apple errors
还有一个简短的映射版本:
let newTokens = tokens.map { Int([=11=]) != nil ? "Apple" : [=11=] }
Swift 3
componentsSeparatedByString(_:)
现在是 components(separatedBy:)
,joinWithSeparator(_:)
现在是 joined(separator:)
。
let tokens = originalString.components(separatedBy: " ")
let newTokens = tokens.map { Int([=12=]) != nil ? "Apple" : [=12=] }
let result = newTokens.joined(separator: " ")
var str: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
var comp: [AnyObject] = [AnyObject](array: str.componentsSeparatedByString(" "))
for var i = 0; i < comp.count; i++ {
var numberRegex: NSRegularExpression = NSRegularExpression.regularExpressionWithPattern("^[0-9]", options: NSRegularExpressionCaseInsensitive, error: nil)
var regexMatch: Int = numberRegex.numberOfMatchesInString(comp[i], options: 0, range: NSMakeRange(0, (String(comp[i])).length))
if regexMatch != 0 {
comp[i] = "Apple"
}
}
var result: String = comp.componentsJoinedByString(" ")
print(result)
问题:数字的长度可能是 1、200、1000、39 99995 等,需要用文本替换,例如。 "Apple" 与数值的长度比较不同。
let str: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
预期结果="hello i have Apple object of Apple string class with Apple object, similar to Apple errors"
我试过以下代码:
var originalString: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
let strippedString: NSMutableString = NSMutableString(capacity: originalString.characters.count)
var numArray: [String] = []
var locArray: [NSNumber] = []
var scanner: NSScanner = NSScanner(string:originalString)
let numbers: NSCharacterSet = NSCharacterSet(charactersInString: "0123456789")
while scanner.atEnd == false {
var buffer: NSString?
if scanner.scanCharactersFromSet(numbers, intoString: &buffer) {
strippedString.appendString(buffer! as String)
numArray.append(buffer! as String)
locArray.append(scanner.scanLocation)
}
else {
scanner.scanLocation = (scanner.scanLocation + 1)
}
}
for (index, _) in numArray.enumerate() {
var loc : Int = Int(locArray[index] ) - (String(numArray[index]).characters.count)
let len = String(numArray[index]).characters.count
let dupStr = "Apple"
if(index != 0 && len != dupStr.characters.count)
{
loc = loc + (dupStr.characters.count - len) + 1
}
originalString.replaceRange(originalString.startIndex.advancedBy(loc)..<originalString.startIndex.advancedBy(loc + len), with: dupStr)
}
print(originalString)
Swift 2
NSScanner 很棒,但是如果您想要一个更简单的解决方案来完成此任务,您可以使用 componentsSeparatedByString
、map
、Int()
和 joinWithSeparator
,如下所示:
let originalString = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
let tokens = originalString.componentsSeparatedByString(" ")
let newTokens = tokens.map { (token) -> String in
if let _ = Int(token) {
return "Apple"
}
return token
}
let result = newTokens.joinWithSeparator(" ")
print(result)
打印:
hello i have Apple object of Apple string class with Apple object, similar to Apple errors
还有一个简短的映射版本:
let newTokens = tokens.map { Int([=11=]) != nil ? "Apple" : [=11=] }
Swift 3
componentsSeparatedByString(_:)
现在是 components(separatedBy:)
,joinWithSeparator(_:)
现在是 joined(separator:)
。
let tokens = originalString.components(separatedBy: " ")
let newTokens = tokens.map { Int([=12=]) != nil ? "Apple" : [=12=] }
let result = newTokens.joined(separator: " ")
var str: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
var comp: [AnyObject] = [AnyObject](array: str.componentsSeparatedByString(" "))
for var i = 0; i < comp.count; i++ {
var numberRegex: NSRegularExpression = NSRegularExpression.regularExpressionWithPattern("^[0-9]", options: NSRegularExpressionCaseInsensitive, error: nil)
var regexMatch: Int = numberRegex.numberOfMatchesInString(comp[i], options: 0, range: NSMakeRange(0, (String(comp[i])).length))
if regexMatch != 0 {
comp[i] = "Apple"
}
}
var result: String = comp.componentsJoinedByString(" ")
print(result)