码迷,mamicode.com
首页 > 编程语言 > 详细

swift -- 定义空字符串 hasPrefix hasSuffix trim split join range

时间:2015-08-07 00:01:42      阅读:700      评论:0      收藏:0      [点我收藏+]

标签:

// 定义空的字符串

var str1 = ""

var str2 = String()

str1.isEmpty      // 判断字符串是否为空

// 输出字符串中所有的字符

var str3 = "As god name"

for c in str3{

  println(c)

}

Int.max   // Int类型的最大值 

Int.min   // Int类型的最小值

 

var arr1 = ["c", "oc", "ios", "swift"]

var count = 0

for name in arr1{

    if name.hasPrefix("i"){    // hasPrefix 前缀

        count++

        count

    }

}

count = 0

for name in arr1{

    if name.hasSuffix("s"){   // hasSuffix 后缀

        count++

        count

    }

}

var string1:String = "   Hello, swift!!   "

var string2 = "As god name"

 

// uppercaseString   lowercaseString

string2.uppercaseString

string2.lowercaseString

// trim  去掉字符串前后的空白

string1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

string1.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " H"))

 

// split

string2.componentsSeparatedByString(" ")

var string3 = "welcome to play swift! Step-by-Step learn from now"

string3.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-"))

 

// join

var string4 = "-"

string4.join(["one", "two", "three"])

 

var string5 = "--"

var arr = ["ios", "swift", "oc"]

string5.join(arr)

 

var str = "Welcome to play Swift! Step by Step learn Swift language from now"

// range

str.rangeOfString("Step")

str.rangeOfString("Step", options:NSStringCompareOptions.BackwardsSearch)

str.rangeOfString("welcome", options:NSStringCompareOptions.CaseInsensitiveSearch)

 

str.startIndex

str.endIndex

countElements(str)

 

var str = "Hello, playground"

let strRange = Range<String.Index>(start:str.startIndex, end:str.endIndex)

 

swift -- 定义空字符串 hasPrefix hasSuffix trim split join range

标签:

原文地址:http://www.cnblogs.com/lianfu/p/4709326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!