1: 简介
(1):OC中字符串分为两种:可变字符串、不可变字符串。
(2):swift中的字符串String是结构体类型,所以我们的操作都是对结构体的实例值就行操作。
(3):OC中的字符串NSString是类类型,我们做的操作都是对对象进行操作。
(4)可变字符串是不可变字符串的子类,所以可变字符串继承不可变,可以使用不可变字符串的所有方法
2:不可变字符串方法
//1.创建空字符串
var str1: NSString = ""
var str2: NSString = NSString()
//2.创建简单字符串对象
var str3: NSString = "helloworld"
//3.使用格式化的方式创建字符串
//占位符:%d表示Int %@表示字符串 %f表示Float类型 %lf表示Double类型 %C表示字符型(只对OC字符串的字符有效)
var str4: NSString = NSString.init(format: "%@,%d,%.2lf", "中国",75,1234.3456789)
print(str4)
NSLog("%@", str4)
let string = String.init(format: "%2d:%2d:%2d", 12,23,56)
print(string)
//4.遍历字符串对象
//.length获取NSString字符串的字符个数
for i in 0..<str4.length {
//通过下标获取字符串中的字符
NSLog("%C", str4.characterAtIndex(i))
//print("\(str4.characterAtIndex(i))")
}
//5.字符串的比较 (建议使用swift语言中字符串的比较方法)
var string1: NSString = "Hello"
var string2: NSString = "hello"
//swift中字符串的比较 == >= <= !=
if (string1 as String) <= (string2 as String) {
print("左边小于右边")
}
//OC中字符串的比较
if !string1.isEqualToString(string2 as String) {
print("不相等")
}
//OC中比较字符串的大小 区分大小
if string1.compare(string2 as String) == NSComparisonResult.OrderedAscending {
print("左边小于右边")
}
//6.不区分大小写 比较字符串的大小
if string1.compare(string2 as String, options: NSStringCompareOptions.CaseInsensitiveSearch) == NSComparisonResult.OrderedSame {
print("两个字符串相等")
}
//7.字符串查找 查找子串在父串中出现的范围 NSRange类型
//NSRange是结构体 有两个属性location位置 length子串的字符个数
let str_new: NSString = "helloworldhellow"
//从前向后查找
let range1 = str_new.rangeOfString("hello")
if range1.location != NSNotFound {
print("找到了子串\(range1.location),\(range1.length)")
}
//从后向前查找
let range2 = str_new.rangeOfString("hello", options: NSStringCompareOptions.BackwardsSearch)
print(range2.location,range2.length)
//8.字符串与数字之间的转换 --- swift中也具有该方法 只是返回值是可选类型(! 强制解析)
let numStr: NSString = "123"
let int_num = numStr.intValue
print(int_num) //转换成什么类型就调用 --- 类型Value
let str_str = NSString.init(format: "%d", 100)
print(str_str)
//9.大小写转换
var lastStr: NSString = "hello"
print(lastStr.uppercaseString)
lastStr = "HELLO"
print(lastStr.lowercaseString)
//10.字符串的提取
===========重点==============
var superStr: NSString = "123456789qwertyuiasdfghj"
//从index开始(包括index)提取字符串后面的部分
let sub1 = superStr.substringFromIndex(0)
//从字符串开始提取到index之前位置(不包括index)
let sub2 = superStr.substringToIndex(5)
print(sub2)
//在字符串中提取某个范围中的字符串
let sub3 = superStr.substringWithRange(NSRange.init(location: 1, length: 3))
print(sub3)
//11.从文件中读取内容
========重点========
//获取文件路径
var path: String = "/Users/xuli/Desktop/猪之歌.txt"
//读取内容
//1、文件路径
//2、文件内容的编码 UTF8
let contents = try! NSString.init(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(contents)
3.可变字符串方法
//1.创建空的可变字符串
let mutable1 = NSMutableString.init(capacity: 0)
//2.将不可变字符串转化成可变字符串
let mutable2 = NSMutableString.init(string: "hello world")
//3.添加
//追加
mutable2.appendString("中国")
mutable2.appendFormat("%d", 998)
print(mutable2)
//插入
mutable2.insertString("🐱🐶🐰", atIndex: 1)
print(mutable2)
//4.删除
mutable2.deleteCharactersInRange(NSRange.init(location: 0, length: 10))
print(mutable2)
//5.修改
//修改指定索引的元素
mutable2.replaceCharactersInRange(NSRange.init(location: 0, length: 1), withString: "1234567812345678")
print(mutable2)
//修改出现在指定位置的多个子字符串
//1、目标子串
//2、用来替换的子串内容
//3、查找字符串的方向
//4、替代子串的范围
mutable2.replaceOccurrencesOfString("123", withString: "♣️♠️", options: NSStringCompareOptions.BackwardsSearch, range: NSRange.init(location: 0, length: mutable2.length))
print(mutable2)
网友评论