从字符串中提取货币
Extracting currency from string
这是Swift 2.1.
如何从类似于 "add egg (£2.00)" 的字符串中提取金额?在这个例子中,我需要“2.00”部分。
查找括号中的内容会不会是一个硬检查?或者有更有效的方法吗? IE。正则表达式之类的?
var myString = "add egg (£2.00)"
myString = myString.stringByReplacingOccurrencesOfString("", withString: "")
let components = myString.componentsSeparatedByString("add egg (£")
let finalString = components[1].stringByReplacingOccurrencesOfString(")", withString: "")
print(finalString)
//这会打印 2.00
有很多方法可以实现您想要的 - 这是一个使用正则表达式的简单示例。
我使用 (?<=\()[^\)]+
来查找 (
和 )
之间的任何内容,然后我使用几个范围来提取值:一个用于货币符号,另一个用于值。
extension String {
func extractValueBetweenParenthesis() -> (currency: String?, value: String?) {
if let range = self.rangeOfString("(?<=\()[^\)]+", options: .RegularExpressionSearch) {
let cr = range.startIndex..<range.startIndex.advancedBy(1)
let vr = range.startIndex.advancedBy(1)..<range.endIndex
return (currency: self.substringWithRange(cr), value: self.substringWithRange(vr))
}
return (nil, nil)
}
}
对字符串调用该方法,然后安全地解包可选结果:
let str = "add egg (£2.00)"
let result = str.extractValueBetweenParenthesis()
if let currency = result.currency, value = result.value {
print("Currency is '\(currency)' and value is '\(value)'")
}
打印:
Currency is '£' and value is '2.00'
'pure' Swift 解决方案,不需要括号
let str = ["add egg £ 2.00",
"the price is .00 per unit",
"send €10.22 to somebody",
"invalid $(12)"]
func value(str: String, currency: String)->Double {
var chars = str.characters
let currs = currency.characters
while !currs.contains(chars.popFirst() ?? " ") {}
let arr = chars.split(" ")
guard let value = arr.first,
let d = Double(String(value)) else { return Double.NaN }
return d
}
let values = str.flatMap { value([=10=], currency: "£$€") }
print(values)
/*
[2.0, 12.0, 10.220000000000001, nan]
*/
如果你真的需要括号,没问题...
let str = ["add egg (£2.00)",
"the price is (.00) per unit",
"send (€10.22) to somebody",
"invalid ($-12)"]
func value(str: String, currency: String)->Double {
var chars = str.characters
let currs = currency.characters
while !currs.contains(chars.popFirst() ?? " ") {}
let arr = chars.split(")")
guard let value = arr.first,
let d = Double(String(value)) else { return Double.NaN }
return d
}
let values = str.flatMap { value([=11=], currency: "£$€") }
print(values)
/*
[2.0, 12.0, 10.220000000000001, -12.0]
*/
这是Swift 2.1.
如何从类似于 "add egg (£2.00)" 的字符串中提取金额?在这个例子中,我需要“2.00”部分。
查找括号中的内容会不会是一个硬检查?或者有更有效的方法吗? IE。正则表达式之类的?
var myString = "add egg (£2.00)"
myString = myString.stringByReplacingOccurrencesOfString("", withString: "")
let components = myString.componentsSeparatedByString("add egg (£")
let finalString = components[1].stringByReplacingOccurrencesOfString(")", withString: "")
print(finalString)
//这会打印 2.00
有很多方法可以实现您想要的 - 这是一个使用正则表达式的简单示例。
我使用 (?<=\()[^\)]+
来查找 (
和 )
之间的任何内容,然后我使用几个范围来提取值:一个用于货币符号,另一个用于值。
extension String {
func extractValueBetweenParenthesis() -> (currency: String?, value: String?) {
if let range = self.rangeOfString("(?<=\()[^\)]+", options: .RegularExpressionSearch) {
let cr = range.startIndex..<range.startIndex.advancedBy(1)
let vr = range.startIndex.advancedBy(1)..<range.endIndex
return (currency: self.substringWithRange(cr), value: self.substringWithRange(vr))
}
return (nil, nil)
}
}
对字符串调用该方法,然后安全地解包可选结果:
let str = "add egg (£2.00)"
let result = str.extractValueBetweenParenthesis()
if let currency = result.currency, value = result.value {
print("Currency is '\(currency)' and value is '\(value)'")
}
打印:
Currency is '£' and value is '2.00'
'pure' Swift 解决方案,不需要括号
let str = ["add egg £ 2.00",
"the price is .00 per unit",
"send €10.22 to somebody",
"invalid $(12)"]
func value(str: String, currency: String)->Double {
var chars = str.characters
let currs = currency.characters
while !currs.contains(chars.popFirst() ?? " ") {}
let arr = chars.split(" ")
guard let value = arr.first,
let d = Double(String(value)) else { return Double.NaN }
return d
}
let values = str.flatMap { value([=10=], currency: "£$€") }
print(values)
/*
[2.0, 12.0, 10.220000000000001, nan]
*/
如果你真的需要括号,没问题...
let str = ["add egg (£2.00)",
"the price is (.00) per unit",
"send (€10.22) to somebody",
"invalid ($-12)"]
func value(str: String, currency: String)->Double {
var chars = str.characters
let currs = currency.characters
while !currs.contains(chars.popFirst() ?? " ") {}
let arr = chars.split(")")
guard let value = arr.first,
let d = Double(String(value)) else { return Double.NaN }
return d
}
let values = str.flatMap { value([=11=], currency: "£$€") }
print(values)
/*
[2.0, 12.0, 10.220000000000001, -12.0]
*/