本节知识点
- 字符
- 字符串
- 字符串常用方法
- 遍历字符串中的所有字符
- 计算字符串长度
- 字符串拼接
- 格式化字符串
- 字符串比较
- 判断前后缀
- 大小写转换
- 转换为基本数据类型
- 字符串和数组转换
- 字符串包含索引
- 字符串替换
- 字符串的截取
1. 字符
char charValue = 'a';//OC当中的字符:
var charValue1:Character = "a" //swift当中的字符:
-
Swift和OC字符不一样
- 1.Swift是用双引号
- 2.Swift中的字符类型和OC中的也不一样
- OC中的字符占一个字节,因为它只包含ASCII表中的字符
- Swift中的字符除了可以存储ASCII表中的字符还可以存储unicode字符.
- OC的字符是遵守ASCII标准的,
- Swift的字符是遵守unicode标准, 所以可以存放世界上所有国家语言的字符(大部分)
char charValue = '豪'; //OC当中:错误
var charValue2:Character = "豪" //swift当中: 正确
var charValue3:Character = "ab" // 直接报错
2. 字符串
//C:
char *stringValue = "ab";
char stringArr = "ab";
//OC:
NSString *stringValue = "ab";
//Swift
var stringValue1 = "ab"
char *stringValue = "abc\0bcd";
printf("%s", stringValue);
//输出结果: abc
NSString *stringValue = @"abc\0bcd";
NSLog(@"%@", stringValue);
- swift当中的字符串和C语言/OC语言中的字符串是不一样的, 不是一
\0
结束
var stringValue = "abc\0bcd"
print(stringValue)
//输入结果: abcbcd
3. 字符串常用方法
3.1 遍历字符串中的所有字符
let str = "CD豪"
// 遍历字符串中所有的字符
for c in str.characters{
print(c)
}
// 输出结果:
//C
//D
//豪
3.2 计算字符串长度
//C:
char *stringValue = "CD豪";
printf("%tu", strlen(stringValue));
//输出结果:5
//OC:
NSString *stringValue = @"CD豪";
NSLog(@"%tu", stringValue.length);
//输出结果:3 以UTF16计算
let str = "CD豪"
// 获取字符串的长度
let length = str.characters.count // 计算字符个数
let blen = str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)// 计算字符串的字节数
print(length) // 3
print(blen) // 5
3.3 字符串拼接
//C:
char str1[] = "abc";
char *str2 = "bcd";
char *str = strcat(str1, str2);
printf("%s",str);
//输出结果: abcbcd
//OC:
NSMutableString *str1 = [NSMutableString stringWithString:@"abc"];
NSString *str2 = @"bcd";
[str1 appendString:str2];
NSLog(@"%@", str1);
//输出结果: abcbcd
//swift:
var str1 = "chen";
var str2 = "dehao";
var str = str1 + str2;
print(str)
//输出结果:chendehao
3.4 格式化字符串
//OC:
NSString *str =
[NSMutableString
stringWithFormat:@"http://blog.csdn.net/chendehao001/pic/%tu.png", @10];
NSLog(@"%@", str);
//输入结果: http://blog.csdn.net/chendehao001/pic/10.png
//swift:
var index = 10
var str3 = "http://blog.csdn.net/chendehao001/pic/\(index).png"
print(str3)
//输入结果: http://blog.csdn.net/chendehao001/pic/10.png
// 拼接过程格式化
let min = 1
let second = 3
// 注意 format: ,arguments: 这个方法中的 aguments 不要敲错, 没有提示, 如果写错就不会现相应输出, 也不会报错哦
let timeStr = String(format: "%02d:%02d", arguments: [min,second])
print(timeStr) // 输出结果
3.5 字符串比较
//oc:
NSString *str1 = @"abc";
NSString *str2 = @"abc";
if ([str1 compare:str2] == NSOrderedSame){
NSLog(@"相等");
}else{
NSLog(@"不相等");
}
//输出结果:相等
// oc
NSString *str1 = @"abc";
NSString *str2 = @"abc";
if ([str1 isEqualToString:str2]){
NSLog(@"相等");
}else{
NSLog(@"不相等");
}
//输出结果:相等
//Swift:(== / != / >= / <=), 和C语言的strcmp一样是逐个比较
var str4 = "abc";
var str5 = "abc";
if str4 == str5{
print("相等");
}else{
print("不相等");
}
//输出结果:相等
var str6 = "abd";
var str7 = "abc";
if str6 >= str7{
print("大于等于");
}else{
print("不大于等于");
}
//输出结果: 大于等于
3.6 判断前后缀
//OC:
NSString *str = @"http://blog.csdn.net/chendehao001";
if ([str hasPrefix:@"http"]) {
NSLog(@"是url");
}
if ([str hasSuffix:@"chendehao001"]) {
NSLog(@"是顶级域名");
}
//输出结果:
//是url
//是顶级域名
//Swift:
var str8 = "http://blog.csdn.net/chendehao001"
if str8.hasPrefix("http") {
print("是url");
}
if str8.hasSuffix("chendehao001") {
print("是顶级域名");
}
//输出结果:
//是url
//是顶级域名
3.7 大小写转换
//OC:
NSString *str = @"abc.txt";
NSLog(@"%@", [str uppercaseString]);
NSLog(@"%@", [str lowercaseString]);
//输出结果: ABC.TXT
//abc.txt
// 转换大小写
var str8 = "chendehao.TEXT"
print(str8.uppercaseString) // 输出结果 CHENDEHAO.TEXT
print(str8.lowercaseString) // 输出结果 chendehao.text
var str1 = "Chen"
var str2 = "dehao"
print(str1.capitalizedString) // 输出结果 : Chen
print(str2.capitalizedString) // 输出结果 : Dehao
3.8 转换为基本数据类型
//OC:
NSString *str = @"250";
NSInteger number = [str integerValue];
NSLog(@"%tu", number);
//输出结果: 250
// swift
//如果str不能转换为整数, 那么可选类型返回nil
var str10 = "250"
//str10 = "250sb" 不能转换所以可能为nil
var number:Int? = Int(str10)
if number != nil{
//以前的版本println会自动拆包, 现在的不会
print(number!)
}
var str9 = "250"
var number1: Int? = Int(str9) // Optional(250)
var number1fromStr = (str9 as NSString).integerValue // 250
var number2: Int = Int(str9)! //250
var number3 = Int(str9) //Optional(250)
var str10 = "250sb"
var number4:Int? = Int(str10) // nil
var number5 = Int(str10) // nil
var str11 = "100"
var sr1 = (str11 as NSString).integerValue //Swift 2.0 //=100
var a = (str11 as NSString).intValue //=100
var l = (str11 as NSString).longLongValue //=100
var str12 = "10.8"
var f = (str12 as NSString).floatValue //=10.8
var d = (str12 as NSString).doubleValue //=10.8
var str13 = "true"
var b = (str13 as NSString).boolValue //=true // BOOL 类型
3.9 字符串和数组转换
var str1WillTransform = "boy, girl, man, woman"
let strTransformArray = str1WillTransform.componentsSeparatedByString(",")
print(strTransformArray) // ["boy", " girl", " man", " woman"]
var str2WillTransform = "boy,girl,man 10 20 30"
let charset = NSCharacterSet(charactersInString:", ") // 注意: 双引号里有逗号和空格
let str2Array = str2WillTransform.componentsSeparatedByCharactersInSet(charset)
print(str2Array) //["boy", "girl", "man", "10", "20", "30"]
3.10 字符串包含索引
//字符串包含
var str2 = "dehao"
var str5 = "chendehao"
let rangeOfString = str5.rangeOfString(str2)
print(rangeOfString!)// 结果: 4..<9
let startIndex = rangeOfString?.startIndex
print(startIndex!) // 结果: 4
3.11 字符串替换 replace
var strReplace = "My name is Aarak"
// swift3.0 将移除的的写法
let subRange = Range(start: strReplace.startIndex,end: strReplace.startIndex.advancedBy(2))
strReplace.replaceRange(subRange, with: "Your")
print(strReplace) // Your name is Aarak
// 推荐写法
let subRange1 = Range(strReplace.startIndex ..< strReplace.startIndex.advancedBy(4))
strReplace.replaceRange( subRange1, with: "CDH")
print(strReplace) // CDH name is Aarak
// 区分大小写搜索与替换
var str2Replace = strReplace.stringByReplacingOccurrencesOfString("Aarak", withString: "CD豪")
print(str2Replace) // CDH name is Aarak --> CDH name is CD豪
// 不分大小写搜索与替换
str2Replace = strReplace.stringByReplacingOccurrencesOfString("aaraK", withString: "DeHAO陈",options: NSStringCompareOptions.CaseInsensitiveSearch,range: nil)
print(str2Replace) // CDH name is Aarak --> CDH name is DeHAO陈
3.12 字符串的截取
// 截取字符, 方式一:
let urlString = "http://blog.csdn.net/chendehao001"
let header = (urlString as NSString).substringToIndex(4) // http
let footer = (urlString as NSString).substringFromIndex(21) // chendehao001
let range = NSMakeRange(21, 9) //
let middle = (urlString as NSString).substringWithRange(range) // chendehao
print(middle) //chendehao
// 截取字符, 方式二:
let endIndex = urlString.startIndex.advancedBy(21)
let starIndex = urlString.endIndex.advancedBy(-3)
let header1 = urlString.substringToIndex(endIndex)
let footer1 = urlString.substringFromIndex(starIndex)
// Range(start: ,end: ) 这个写法在 Swift3.0 移除
let range1 = Range(start: endIndex.advancedBy(0),end: starIndex.advancedBy(-0))
let middle1 = urlString.substringWithRange(range1)
print(middle1) // chendehao
let range2 = Range( urlString.startIndex.advancedBy(21) ..< urlString.endIndex.advancedBy(-3))
let middle2 = urlString.substringWithRange(range2)
print(middle2) // chendehao
网友评论