此String扩展中的toCamel()
函数应该删除任何_
后跟字符串中间的字符,并将两个字符替换为大写。
public extension String {
public func rangeFromNSRange(aRange: NSRange) -> Range<String.Index> {
let s = advance(self.startIndex, aRange.location)
let e = advance(self.startIndex, aRange.location + aRange.length)
return s..<e
}
public var ns : NSString {return self as NSString}
public subscript (aRange: NSRange) -> String? {
get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
}
public var cdr: String {return isEmpty ? "" : String(characters.dropFirst())}
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_.", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range)
let uc = s[match.range]!.uppercaseString.cdr
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
}
问题是matchesInString
没有返回_
已经属于上一场比赛的比赛。因此,它得出以下结果:
try "hello_world".toCamel() //"helloWorld"
try "hello__world".toCamel() //"hello_world"
try "hello___world".toCamel() //"hello_World"
如何解决这个问题?
答案 0 :(得分:4)
将您的模式更改为_+(.)
。这匹配一个或多个下划线,后跟另一个字符。圆括号将该角色放入捕获组。您可以使用rangeAtIndex
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_+(.)", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range) // the whole match range
let replaceRange = match.rangeAtIndex(1) // range of the capture group
let uc = s[replaceRange]!.uppercaseString
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
示例:
"hello_world".toCamel() // helloWorld
"hello___world".toCamel() // helloWorld
"hello_beautiful____world".toCamel() // helloBeautifulWolrd
public extension String {
public func rangeFromNSRange(aRange: NSRange) -> Range<String.Index> {
let s = self.startIndex.advancedBy(aRange.location)
let e = self.startIndex.advancedBy(aRange.location + aRange.length)
return s..<e
}
public var ns : NSString {return self as NSString}
public subscript (aRange: NSRange) -> String? {
get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
}
public var cdr: String {return isEmpty ? "" : String(characters.dropFirst())}
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_+(.)", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range) // the whole match range
let replaceRange = match.rangeAtIndex(1) // range of the capture group
let uc = s[replaceRange]!.uppercaseString
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
}
答案 1 :(得分:2)
您的代码中存在两个问题。
首先,符合您要求的正则表达式应为_+[^_]{1}
。
在您的cdr
计算保护的第二位,String(characters.dropFirst())
仅排除第一个_
字符。您可以使用String(characters.suffix(1))
编辑代码:
public extension String {
public func rangeFromNSRange(aRange: NSRange) -> Range<String.Index> {
let s = advance(self.startIndex, aRange.location)
let e = advance(self.startIndex, aRange.location + aRange.length)
return s..<e
}
public var ns : NSString {return self as NSString}
public subscript (aRange: NSRange) -> String? {
get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
}
public var cdr: String {return isEmpty ? "" : String(characters.suffix(1))}
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_+[^_]{1}", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range)
let uc = s[match.range]!.uppercaseString.cdr
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
}
我在操场上测试了这个。