//字符串操作
//1 求字符串长度
NSString * str = @"hello world";
NSUInteger len = [str length];//unsigned long
NSLog(@"len = %lu",len);
2 把输入的字符串转化为OC字符串
char ch[100] = {};
//abzzzdf ab
scanf("%[abc]",ch);//ch = ab a = 1 a = 2
scanf("%[^\n]",ch); //ch = zzzdf;
// a b '1' '\n'
//扫描集形式%[] 括号里边是条件
// ^字符 当做条件 除了这个字符 都接受 直到碰到此字符 接收结束
//把C字符串 转换为OC
NSString *str1 = [NSString stringWithUTF8String:ch];
NSLog(@"str1 = %@",str1);
//3 获取某个位置的字符
//char %c unichar %C
unichar ch =[str characterAtIndex:0];
NSLog(@"%C",ch);
//4 把单个字符转化为字符串
NSString * strChar = [NSString stringWithFormat:@"%C",ch];
NSLog(@"strChar = %@",strChar);
//5 提取子串 从0提取到index 不包含index
//hello world
NSString *subStr1 = [str substringToIndex:4];
NSLog(@"substr1 = %@",subStr1);
//6 提取子串 从index到末尾 包含index
NSString *subStr2 = [str substringFromIndex:2];
NSLog(@"subStr2 = %@",subStr2);
7 提取范围字符子串
NSRange range1 = {3,4};
//NSRange 定义一个范围 {A,B} 从A开始 B个位置
//NSString 开头类方法 构造一个对象 对象开头 对此对象进行操作
NSString *subStr3 = [str substringWithRange:range1];
NSLog(@"subStr3 =%@",subStr3);
8 判断是否含有前缀
NSString *str1 = @"www.baidu.com";
BOOL ret = [str1 hasPrefix:@"www"];
if (ret){
NSLog(@"str1含有www前缀");
}else{
NSLog(@"str1不含有前缀");
}
if ([str1 hasSuffix:@"com"]){
NSLog(@"str1含有后缀com");
} else{
NSLog(@"str不含有后缀");
}
9 字符串转换成大写字符
NSString *str2 = @"asdfgQWERTxcvb";
NSString * str3 = [str2 uppercaseString];
NSLog(@"str3 = %@",str3);
9.1字符串转换成小写字符
NSString *str4 = [str2 lowercaseString];
NSLog(@"str4 = %@",str4);
9.2把字符串首字母大写
NSString *str5 = [str2 capitalizedString];
NSLog(@"str5 = %@",str5);
10 把字符串转换成基本数据类型
NSString *str6 = @"123";//floatValue intgerValue
NSLog(@"%i",[str6 intValue]);
11 字符串比较
NSString *str1 = @"ABC";
NSString *str2 = @"abc";
NSComparisonResult ret = [str1 compare:str2];
//str1-str2 0 1
11.1 升序 NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending
if (ret == NSOrderedAscending)
{
NSLog(@"str1 <str2");
}
else if(ret == NSOrderedSame)
{
NSLog(@"str1 = str2");
}
else
{
NSLog(@"str1>str2");
}
/*
NSCaseInsensitiveSearch = 1, 以大小写不敏感形式进行比较
NSLiteralSearch = 2, 正序查找
NSBackwardsSearch = 4, 反序查找
*/
NSComparisonResult ret1 = [str1 compare:str2 options:NSCaseInsensitiveSearch];
if (ret1 == 0)
{
NSLog(@"大小写不敏感");
}
12 判断字符串是够相等
BOOL ret2 =[str1 isEqualToString:str2];
if (ret2 == 0)
{
NSLog(@"str1 不等于 str2");
}
13 字符串查找
NSString * str = @"nanjing boweifeng nanjing";
NSRange range1 =[str rangeOfString:@"jing"];
//range1.location 字符串的下标 range1.length 字符串的长度
if (range1.location== NSNotFound)
{
NSLog(@"没有查找到");
}
else
{
NSLog(@"location =%li length =%li",range1.location,range1.length);
}
13.1 选择正序或者逆序查找
NSRange range2 = [str rangeOfString:@"jing" options:NSBackwardsSearch];
if (range2.location== NSNotFound)
{
NSLog(@"没有查找到");
}
else
{
NSLog(@"location =%li length =%li",range2.location,range2.length);
}
13.2 在指定范围内 正序或者逆序查找 1 要查找的字符串 2 添加一个条件正序或者逆序 3 在指定范围内nanjing boweifeng nanjing
NSRange range3 = [str rangeOfString:@"feng" options:NSLiteralSearch range:NSMakeRange(8, 10)];
NSLog(@"location =%li length =%li",range3.location,range3.length);
14 替换
// 1 使用某个字符串替换字符串
NSString *str3 = [str stringByReplacingOccurrencesOfString:@"nanjing" withString:@"替换"];
NSLog(@"str3 = %@",str3);
//2 在某个范围内 正序或者逆序 使用某个字符串替换字符串
NSString *str4 = [str stringByReplacingOccurrencesOfString:@"nanjing" withString:@"替换" options:NSBackwardsSearch range:NSMakeRange(10, 15)];
NSLog(@"str4 = %@",str4);
//3
NSString *str5 = [str stringByReplacingCharactersInRange:NSMakeRange(5, 7) withString:@"替换范围"];
NSLog(@"str5 =%@",str5);
15 读取网址
NSURL * url =[NSURL URLWithString:@"http://www.douyu.com"];
//1 网址 2 编码要求 3 nil
NSString *urlString = [NSString stringWithContentsOfURL:url encoding:4 error:nil];
NSLog(@"urlString =%@",urlString);
16 读取文件
NSString *fileString = @"/Users/wangjiawei/Desktop/git命令.rtf";
NSString *fileContent = [NSString stringWithContentsOfFile:fileString encoding:4 error:nil];
NSLog(@"fileContent = %@",fileContent);