Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        3年前关闭。
                                                                                            
                
        
嗨,我有一个值是


  块引用
  1 1:0.0644343 1 1:0.0309334 1 1:0.0261616
  块引用


我想按空格分隔值,但在某些字符后得到这样的结果。是否有任何可能的解决方案。我知道我们可以在正则表达式中做


  块引用
  “ 1 1:0.0644343”
  “ 1 1:0.0309334”
  “ 1 1:0.0261616”
  块引用

最佳答案

我认为正则表达式是这里的理想工具:

var str = "Blockquote 1 1:0.0644343 1 1:0.0309334 1 1:0.0261616 Blockquote"

let regex = try! NSRegularExpression(pattern: "(\\d \\d:[\\.0-9]+)", options: [])
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))

for m in matches.reverse() {
    let range = m.rangeAtIndex(1)
    let startIndex = str.startIndex.advancedBy(range.location)
    let endindex = startIndex.advancedBy(range.length)
    let value = str[startIndex..<endindex]

    str.replaceRange(startIndex..<endindex, with: "\"\(str[startIndex..<endindex])\"")
}

print(str)

关于regex - Swift中某些字符后的空格分隔值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38386000/

10-09 20:15