转:http://www.cocoachina.com/bbs/read.PHP?tid=207275
Swift String扩展
/*
* @String扩展
* @Date:2014/06/17
*/
importCocoa
extensionString{
//分割字符
funcsplit(s:String)->String[]{
ifs.isEmpty{
varx=String[]()
foryinself{
x.append(String(y))
}
returnx
}
returnself.componentsSeparatedByString(s)
}
//去掉左右空格
functrim()->String{
returnself.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
//是否包含字符串
funchas(s:String)->Bool{
ifself.rangeOfString(s) {
returntrue
}else{
returnfalse
}
}
//是否包含前缀
funchasBegin(s:String)->Bool{
ifself.hasPrefix(s) {
return//是否包含后缀
funchasEnd(s:String)->Bool{
ifself.hasSuffix(s) {
return//统计长度
funclength()->Int{
returncountElements(self)
}
//统计长度(别名)
funcsize()->Int{
returncountElements(//截取字符串
funcsubstr(range:Int...)->String{
ifrange[0]==0{
returnself.substringToIndex(range[1])
}else{
returnself.substringFromIndex(range[0])
}
}
//重复字符串
funcrepeat(times: Int)->String{
varresult=""
foriin0..times {
result+=self
}
returnresult
}
//反转
funcreverse()->String{
vars=self.split("").reverse()
varx=""
foryins{
x+=y
}
returnx
}
}
vartest="yckit.com "
test.split(".")
test.trim()
test.has("yckit")
test.hasBegin("yckit")
test.hasEnd("com")
test.length()
test.size()
varr="卧槽"
r.repeat(3)
vartest2="DMKU.NET"
test2.substr(0,1)
test2.substr(1,153)">2)
test2.reverse()
https://github.com/jinzhe/swift