首页 > 编程语言 > 详细

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

时间:2015-08-06 23:59:57      阅读:528      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!