alp*_*nse 19 string integer character swift
我需要从字符串中提取数字并将它们放入Swift中的新数组中.
var str = "I have to buy 3 apples, 7 bananas, 10eggs"
Run Code Online (Sandbox Code Playgroud)
我试图循环每个字符,我不知道比较字符和Int.
Geo*_*dze 43
斯威夫特3/4
let string = "0kaksd020dk2kfj2123"
if let number = Int(string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()) {
// Do something with this number
}
Run Code Online (Sandbox Code Playgroud)
您还可以进行如下扩展:
extension Int {
static func parse(from string: String) -> Int? {
return Int(string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined())
}
}
Run Code Online (Sandbox Code Playgroud)
然后用它像:
if let number = Int.parse(from: "0kaksd020dk2kfj2123") {
// Do something with this number
}
Run Code Online (Sandbox Code Playgroud)
Vas*_*rov 25
首先,我们拆分字符串,以便我们可以处理单个项目.然后我们NSCharacterSet用来选择数字.
import Foundation
let str = "I have to buy 3 apples, 7 bananas, 10eggs"
let strArr = str.split(separator: " ")
for item in strArr {
let part = item.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
if let intVal = Int(part) {
print("this is a number -> \(intVal)")
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
let string = "I have to buy 3 apples, 7 bananas, 10eggs"
let stringArray = string.components(separatedBy: CharacterSet.decimalDigits.inverted)
for item in stringArray {
if let number = Int(item) {
print("number: \(number)")
}
}
Run Code Online (Sandbox Code Playgroud)
Hus*_*eem 10
let str = "Hello 1, World 62"
let intString = str.componentsSeparatedByCharactersInSet(
NSCharacterSet
.decimalDigitCharacterSet()
.invertedSet)
.joinWithSeparator("")
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个包含所有数字的字符串,然后您可以这样做:
let int = Int(intString)
Run Code Online (Sandbox Code Playgroud)
只需确保打开它,因为它let int = Int(intString)是可选的.
使用Swift提取正则表达式中的"正则表达式辅助函数" 匹配:
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
let regex = NSRegularExpression(pattern: regex,
options: nil, error: nil)!
let nsString = text as NSString
let results = regex.matchesInString(text,
options: nil, range: NSMakeRange(0, nsString.length))
as! [NSTextCheckingResult]
return map(results) { nsString.substringWithRange($0.range)}
}
Run Code Online (Sandbox Code Playgroud)
你可以轻松实现这一目标
let str = "I have to buy 3 apples, 7 bananas, 10eggs"
let numbersAsStrings = matchesForRegexInText("\\d+", str) // [String]
let numbersAsInts = numbersAsStrings.map { $0.toInt()! } // [Int]
println(numbersAsInts) // [3, 7, 10]
Run Code Online (Sandbox Code Playgroud)
该模式"\d+"匹配一个或多个十进制数字.
当然,如果您因为某种原因而不喜欢使用辅助函数,则可以完成相同的操作:
let str = "I have to buy 3 apples, 7 bananas, 10eggs"
let regex = NSRegularExpression(pattern: "\\d+", options: nil, error: nil)!
let nsString = str as NSString
let results = regex.matchesInString(str, options: nil, range: NSMakeRange(0, nsString.length))
as! [NSTextCheckingResult]
let numbers = map(results) { nsString.substringWithRange($0.range).toInt()! }
println(numbers) // [3, 7, 10]
Run Code Online (Sandbox Code Playgroud)
没有正则表达式的替代方案
let str = "I have to buy 3 apples, 7 bananas, 10eggs"
let digits = "0123456789"
let numbers = split(str, allowEmptySlices: false) { !contains(digits, $0) }
.map { $0.toInt()! }
println(numbers) // [3, 7, 10]
Run Code Online (Sandbox Code Playgroud)
对我来说,将它作为字符串扩展更有意义,可能这是一个品味问题:
extension String {
func parseToInt() -> Int? {
return Int(self.components(separatedBy: CharacterSet.decimalDigits.inverted).joined())
}
}
Run Code Online (Sandbox Code Playgroud)
所以可以这样使用:
if let number = "0kaksd020dk2kfj2123".parseToInt() {
// Do something with this number
}
Run Code Online (Sandbox Code Playgroud)
小智 5
根据@flashadvanced 的回答,我发现以下内容对我来说更短更简单。
let str = "I have to buy 3 apples, 7 bananas, 10eggs"
let component = str.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
let list = component.filter({ $0 != "" }) // filter out all the empty strings in the component
print(list)
Run Code Online (Sandbox Code Playgroud)
在操场上试过,效果很好
希望能帮助到你 :)
| 归档时间: |
|
| 查看次数: |
26031 次 |
| 最近记录: |