我怎样才能trim swift 末尾的数字?

How can I trim the numbers at the end on the swift?

我想删除字符串末尾的数字,但我做不到。我该怎么做?

示例:

swift 23 
ios   36
iphone 25

swift
ios
iphone

但我也想在开头显示数字。

示例:

25 january 2018 my question
11 september 2001 

更新

这是我的代码:

 func parseHTML(html: String) -> Void { 
if let doc = try? HTML(html: html, encoding: .utf8) {
var showString = String() 
for show in doc.css("ul[class^='topic'] li a"){ 
showString = show.text!.trimmingCharacters(in: CharacterSet.decimalDigits) //it's remove whole numbers  
goster.append(showString) 
}

更新

问题已更改,因此此答案现在可能不适合您。


如果中间总是有 space 或更多,一个快速的方法是:

var str = "swift 23"
let newString = str.split(separator: " ").first

您可以将 String.replacingOccurences(of:,with:,options:) 与正则表达式一起使用,以仅匹配字符串末尾的数字。

let stringsWithNumbers = ["swift 23", "ios   36", "iphone 25","25 january 2018 my question","11 september 2001"]
let newStrings = stringsWithNumbers.map({[=10=].replacingOccurrences(of: "\d+$", with: "", options: .regularExpression)})
print(newStrings)

输出:

["swift ", "ios ", "iphone ", "25 january 2018 my question", "11 september "]

如果您还想删除字符串末尾的空格,只需将正则表达式更改为 "\s*\d+$"