本帖最后由 ★彬★卍囧神 于 2014-9-18 22:53 编辑
花了一些时间总结了NSString常用的方法,希望对大家学习上有用!!!void string(){ // 创建一个空的字符串 NSString *str = [[NSString alloc] init]; str = @"asdasda"; // 创建一个可传参数的字符串 NSString *stra = [NSString stringWithFormat:@"wangbin"]; stra = nil; // C语言转成NSString char *s = "LOVE"; NSString *strc = [[NSString alloc] initWithUTF8String:s];
NSLog(@"%@", strc);
// 是否合另一个字符串相同 NSString *str1 = @"JIAYOU!!!"; NSString *str2 = @"JIAYOU!!!"; [str1 isEqualToString:str2];
NSString *stt = @"wangbin"; [stt uppercaseString]; // 转大写 [stt lowercaseString]; // 转小写 [stt capitalizedString]; // 首字母 [stt hasPrefix:@"1213"]; // 判断前缀是以什么开头 [stt hasSuffix:@"741"]; // 判断后缀是以什么结尾
NSRange range = [stt rangeOfString:@"bin"]; // 查找范围 // 将结构体包装成数组 NSString *nsstr = NSStringFromRange(range); NSLog(@"%@", nsstr);
stt = @"jiayou!!!"; NSLog(@"%@", [stt substringFromIndex:1]); // 截取当前位置到尾部 NSLog(@"%@", [stt substringToIndex:1]); // 截取头部到当前位置-1 NSLog(@"%@", [stt substringWithRange:NSMakeRange(3,3)]); // 截取指定位置结果包含这个位置的元素 #pragma mark -字符串中文件路径有关方法 // 1.加上.后缀 NSString *Path = @"binbin"; NSString *addPath = [Path stringByAppendingPathExtension:@"txt"]; // 输出binbin.txt NSLog(@"%@", addPath);
// 2.添加全路径 NSString *Path1 = @"~/NSData.txt"; NSString *absolutePath = [Path1 stringByExpandingTildeInPath]; // 输出absolutePath:/Users/binbin/NSData.txt NSLog(@"absolutePath:%@",absolutePath);
// 3.去掉全路径 NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); // 输出Path:~/NSData.txt
// 4.文件扩展名 NSString *str11 = @"~/binbin.txt"; NSLog(@"%@", [str11 pathExtension]); // 输出txt #pragma mark 字符串的类型转换 // 将NSString转成基础类型 NSString *strr1 = @"100"; int a = [strr1 intValue]; const char *s1 = [strr1 UTF8String]; NSLog(@"%d-%s", a, s1);
// 将NSString转成数组,数组转成字符串 NSString *strr2 = @"wo shi hao ren"; NSArray *arr = [strr2 componentsSeparatedByString:@" "]; NSLog(@"%@", arr); NSString * strr3 = [arr componentsJoinedByString:@"-"]; NSLog(@"%@", strr3); #pragma mark -字符串的拼接 // 字符串的拼接 NSString *strr4 = @"zhang"; NSString *strr5 = @"san"; NSString *strr6 = [NSString stringWithFormat:@"%@%@", strr4, strr5]; NSString *strr7 = [strr4 stringByAppendingString:strr5]; NSString *strr8 = [strr4 stringByAppendingFormat:@"%@", strr5]; NSLog(@"%@-%@-%@",strr6, strr7, strr8); #pragma mark -字符串的比较
// compare比较方法 NSString *str3 = @"JIAYOU!!!"; NSString *str4 = @"JIAYOU!!!"; [str3 compare:str4]; // NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending
//不考虑大小写比较字符串第一种 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02]; NSLog(@"result:%d",result);
//不考虑大小写比较字符串第二种2 NSString *astring03 = @"abc"; NSString *astring04 = @"bbc"; BOOL result1 = [astring03 compare:astring04 options:NSLiteralSearch]; NSLog(@"result1:%d",result1);
/* NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较(比较首元素) NSNumericSearch:进行完全比较。 */
#pragma mark -可变字符串一些方法 //可变字符串对象:NSMutableString继承至string NSMutableString *mutableString1 = [[NSMutableString alloc]initWithFormat:@"abc"]; NSLog(@"%@",mutableString1); //对可变字符串赋值 [mutableString1 setString:@"qianfeng"]; NSLog(@"%@",mutableString1); //插入一个字符串,在指定下标(不要越界)位置插入NSString类型字符 [mutableString1 insertString:@"...xyz" atIndex:3]; NSLog(@"%@",mutableString1); //在字符串末尾追加字符串 [mutableString1 appendString:@"123"]; NSLog(@"%@",mutableString1); //替换 NSRange range2={1,3}; [mutableString1 replaceCharactersInRange:range2 withString:@"efg"]; NSLog(@"%@",mutableString1); //删除 [mutableString1 deleteCharactersInRange:range2]; NSLog(@"%@",mutableString1);
} 下面是可复制的代码- void string(){
- // 创建一个空的字符串
- NSString *str = [[NSString alloc] init];
- str = @"asdasda";
- // 创建一个可传参数的字符串
- NSString *stra = [NSString stringWithFormat:@"wangbin"];
- stra = nil;
- // C语言转成NSString
- char *s = "LOVE";
- NSString *strc = [[NSString alloc] initWithUTF8String:s];
- NSLog(@"%@", strc);
- // 是否合另一个字符串相同
- NSString *str1 = @"JIAYOU!!!";
- NSString *str2 = @"JIAYOU!!!";
- [str1 isEqualToString:str2];
-
- NSString *stt = @"wangbin";
- [stt uppercaseString]; // 转大写
- [stt lowercaseString]; // 转小写
- [stt capitalizedString]; // 首字母
- [stt hasPrefix:@"1213"]; // 判断前缀是以什么开头
- [stt hasSuffix:@"741"]; // 判断后缀是以什么结尾
-
- NSRange range = [stt rangeOfString:@"bin"]; // 查找范围
- // 将结构体包装成数组
- NSString *nsstr = NSStringFromRange(range);
- NSLog(@"%@", nsstr);
-
- stt = @"jiayou!!!";
- NSLog(@"%@", [stt substringFromIndex:1]); // 截取当前位置到尾部
- NSLog(@"%@", [stt substringToIndex:1]); // 截取头部到当前位置-1
- NSLog(@"%@", [stt substringWithRange:NSMakeRange(3,3)]); // 截取指定位置结果包含这个位置的元素
- #pragma mark -字符串中文件路径有关方法
- // 1.加上.后缀
- NSString *Path = @"binbin";
- NSString *addPath = [Path stringByAppendingPathExtension:@"txt"];
- // 输出binbin.txt
- NSLog(@"%@", addPath);
-
- // 2.添加全路径
- NSString *Path1 = @"~/NSData.txt";
- NSString *absolutePath = [Path1 stringByExpandingTildeInPath];
- // 输出absolutePath:/Users/binbin/NSData.txt
- NSLog(@"absolutePath:%@",absolutePath);
-
- // 3.去掉全路径
- NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
- // 输出Path:~/NSData.txt
-
- // 4.文件扩展名
- NSString *str11 = @"~/binbin.txt";
- NSLog(@"%@", [str11 pathExtension]); // 输出txt
- #pragma mark 字符串的类型转换
- // 将NSString转成基础类型
- NSString *strr1 = @"100";
- int a = [strr1 intValue];
- const char *s1 = [strr1 UTF8String];
- NSLog(@"%d-%s", a, s1);
-
- // 将NSString转成数组,数组转成字符串
- NSString *strr2 = @"wo shi hao ren";
- NSArray *arr = [strr2 componentsSeparatedByString:@" "];
- NSLog(@"%@", arr);
- NSString * strr3 = [arr componentsJoinedByString:@"-"];
- NSLog(@"%@", strr3);
- #pragma mark -字符串的拼接
- // 字符串的拼接
- NSString *strr4 = @"zhang";
- NSString *strr5 = @"san";
- NSString *strr6 = [NSString stringWithFormat:@"%@%@", strr4, strr5];
- NSString *strr7 = [strr4 stringByAppendingString:strr5];
- NSString *strr8 = [strr4 stringByAppendingFormat:@"%@", strr5];
- NSLog(@"%@-%@-%@",strr6, strr7, strr8);
- #pragma mark -字符串的比较
-
- // compare比较方法
- NSString *str3 = @"JIAYOU!!!";
- NSString *str4 = @"JIAYOU!!!";
- [str3 compare:str4];
- // NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending
-
- //不考虑大小写比较字符串第一种
- NSString *astring01 = @"this is a String!";
- NSString *astring02 = @"This is a String!";
- BOOL result = [astring01 caseInsensitiveCompare:astring02];
- NSLog(@"result:%d",result);
-
-
- //不考虑大小写比较字符串第二种2
- NSString *astring03 = @"abc";
- NSString *astring04 = @"bbc";
- BOOL result1 = [astring03 compare:astring04
- options:NSLiteralSearch];
- NSLog(@"result1:%d",result1);
-
- /*
- NSCaseInsensitiveSearch:不区分大小写比较
- NSLiteralSearch:进行完全比较(比较首元素)
- NSNumericSearch:进行完全比较。
- */
-
- #pragma mark -可变字符串一些方法
- //可变字符串对象:NSMutableString继承至string
- NSMutableString *mutableString1 = [[NSMutableString alloc]initWithFormat:@"abc"];
- NSLog(@"%@",mutableString1);
- //对可变字符串赋值
- [mutableString1 setString:@"qianfeng"];
- NSLog(@"%@",mutableString1);
- //插入一个字符串,在指定下标(不要越界)位置插入NSString类型字符
- [mutableString1 insertString:@"...xyz" atIndex:3];
- NSLog(@"%@",mutableString1);
- //在字符串末尾追加字符串
- [mutableString1 appendString:@"123"];
- NSLog(@"%@",mutableString1);
- //替换
- NSRange range2={1,3};
- [mutableString1 replaceCharactersInRange:range2 withString:@"efg"];
- NSLog(@"%@",mutableString1);
- //删除
- [mutableString1 deleteCharactersInRange:range2];
- NSLog(@"%@",mutableString1);
- }
复制代码 最后说一句:第三批的朋友加油!!!
|