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

swift字符串_04_字符串基本使用

时间:2016-06-04 11:56:47      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

//: Playground - noun: a place where people can play

import UIKit

//1.创建空字符串
var emptyStr1 = ""
var emptyStr2 = String() //使用构造函数来创建空串

//判断字符串是否为空串
let isEmpty = emptyStr1.isEmpty

//2.字符串的拼接
var name = "Kathy"
let height = String("160")

//(1)使用插值符号进行拼接
let fullString = "\(name)‘s height is \(height)cm"

//(2)运算符+重载
let fullString2 = name + "‘s height is " + height + "cm"

//(3)调用函数
name.appendContentsOf(fullString)

//3.字符串的比较
let str1 = "We are 伐木累"
let str2 = "We are 伐木累"

//相等
if str1 == str2 {
    print("str == str2")
}

//是否包含前后缀
str1.hasPrefix("We are")
str2.hasSuffix("kkk")

//4.获取字符串中的字符
var loopStr = "I am a single ??"
for char in loopStr.characters {
    
    print(char)
}

//5.获取字符串的长度
loopStr.characters.count

//6.根据下标获取到某个字符
loopStr[loopStr.startIndex] //第一个字符
loopStr[loopStr.endIndex.predecessor()] //loopStr.endIndex:末尾的下一个的序号

loopStr[loopStr.endIndex.advancedBy(-5)]

//7.插入删除
loopStr.insert("??", atIndex: loopStr.endIndex)
loopStr.removeAtIndex(loopStr.endIndex.advancedBy(-4))
print(loopStr)

 

swift字符串_04_字符串基本使用

标签:

原文地址:http://www.cnblogs.com/foreveriOS/p/5558298.html

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