- 字符串的截取
NSString *str = @"hello world Wellcome";
//从哪个位置起,截取多少位
NSString *str1 = [str substringWithRange:NSMakeRange(3, 6)];
//从某个位置起,一直到末尾
NSString *str2 = [str substringFromIndex:5];
//从开始截取到某个位置
NSString *str3 = [str substringToIndex:5];
- 字符串的比较
NSString *str = @"hello";
if ([str isEqualToString:@"hello"]){
NSLog(@"isEqual");
}
- 字符串包含...内容(以...结尾,以...开头)
NSString *str = @"http://www.xxx.com";
//字符串包含...
if ([str containsString:@"xxx"]) {
NSLog(@"包含xxx");
}
//字符串以...结尾
if ([str hasSuffix:@".com"]) {
NSLog(@"以.com结尾");
}
//字符串以...开头
if ([str hasPrefix:@"http:"]) {
NSLog(@"以http:开头");
}
- 查询字符串所在位置
NSString *str = @"hello world welcome";
NSRange range = [str rangeOfString:@"welcome"];
//当字符串不存在时
if (range.location == NSNotFound) {
NSLog(@"no found");
}
//字符串所在位置
NSLog(@"%ld", range.location);
- 字符串的连接
//1. 连接时不会自动加"/"
NSString *finalPath1 = [basePath stringByAppendingString: @"/Documents"];
//2. 连接时会自动加上"/"
NSString *finalPath2 = [basePath stringByAppendingPathComponent:@"Documents"];
//3. 最常用的连接
NSString *finalPath3 = [NSString stringWithFormat:@"%@/Documents", basePath];
- 字符串与整数之间的转换
NSString *a = @"55";
//将字符串转换成整数
NSInteger b = a.integerValue;
//将整数转化成数组
NSString *a1 = [NSString stringWithFormat:@"%ld", b];
- 将字符串的形式统一为大写或小写或首字母大写
NSString *name = @"zhangSAN liSI";
//将字符串统一转换成小写
NSString *trueName1 = [name lowercaseString];
//将字符串统一成大写形式
NSString *trueName2 = [name uppercaseString];
//每个单词的首字母大写
NSString *trueName3 = [name capitalizedString];
- NSData和NSString类型的转换
NSString *str = @"hello world";
//将NSString类型转换成NSData类型
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//将NSData类型转换成NSString类型
NSString *data_string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- 去掉字符串之前的空格符号
NSString *userName = @" zhangsan";
//去掉字符串前面的所有空格符号
NSString *finalUserName = [userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- 屏蔽指定单词
NSString *str = @"hello world welcome";
NSString *secrStr = [str stringByReplacingOccurrencesOfString:@"world" withString:@"*****"];
NSLog(@"%@", secrStr);
NSString *sourceStr = @"welcome";
NSString *target;
NSInteger length = sourceStr.length;
NSMutableArray *stars = [NSMutableArray array];
for (int i = 0; i < length; i++) {
[stars addObject:@"*"];
}
target = [stars componentsJoinedByString:@""];
NSString *secrStr2 = [str stringByReplacingOccurrencesOfString:sourceStr withString:target];
NSLog(@"%@", secrStr2);
- 将NSString写入文件
NSString *str = @"牛逼呀!!!!";
NSString *path = @"/Users/apple/Desktop/str.txt";
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *result = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
- 数组和字符串之间的转换
NSString *str = @"asd,weasd,dsadq";
//以","分割成数组
NSArray *arr = [str componentsSeparatedByString:@","];
//以"_"合并成字符串
NSString *str2 = [arr componentsJoinedByString:@"_"];
- 获取左边栏文件的路径
//第一个参数是文件名,第二个参数是文件类型
NSString *lyricsPath = [[NSBundle mainBundle] pathForResource:@"谢谢你的爱" ofType:@"lrc"];