Swift 操作字符串

 

字符串分隔:

var str_componets = "I would prefer a face-to-face talk with my friends"
str_componets.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: " -"))
这样,str_componets分成了:
["I", "would", "prefer", "a", "face", "to", "face", "talk", "with", "my", "friends"]

swift语言的数组提供了一个map函数很好用! 
  可建立一个a数组的映射数组b,即数学上的 y = f(x) 

func fx(x : Int){ 
   reurn x + 10 
}    
var a = [1, 2, 3, 4, 5] 
var b = a.map(fx) 
println(b) 
   
  // [11, 12, 13, 14, 15]

 替换字符串

var str1 = "\"dfasd"
var b = str1.stringByReplacingOccurrencesOfString("\"", withString: "", options: nil, range: nil)

 

posted @ 2015-02-15 10:34  ericjuns  阅读(185)  评论(0编辑  收藏  举报