1、NSString的用法
//1、创建常量字符串。
NSString *string1 = @"这是一个NSString对象";
//2、创建空字符串,给予赋值。实例化一个对象
NSString *string2 = [[NSString alloc] init];
NSString *string = [NSString string];
//3、综合1、2方法,提升性能:initWithString方法
NSString *string3 = [[NSString alloc] initWithString:@"This is a string3"];
NSString *string = [NSString stringWithString:@"创建一个字符串"];
// 用这种方法xcode会让你修改为创建一个字符串常量的结构
//4、用标准C语言字符串创建OC字符串:initWithCString方法(注意编码方式参数的选择)
char *string4c = "This is a C String!中文也支持";
NSString *string4 = [[NSString alloc] initWithCString:string4c encoding:NSUTF8StringEncoding];
// 这种创建方式在从网上获取数据出现“乱码”时,进行转码的时候将会用到。这时,string4c指明编码类型(NSUTF8StringEncoding)
//5、创建格式化字符串:占位符(由一个%加一个字符组成)
int i = 1;
int j = 2;
NSString *string5 = [[NSString alloc] initWithFormat:@"这里输入你想要输入的内容%d",i];
//除了int,float,其他数据类型也可以
从文件读取字符串:initWithContentsOfFile方法
NSString *path6 = @"string8.text"; //@里出现的是想要访问的文件的路径
NSString *string6 = [[NSString alloc] initWithContentsOfFile:path6 encoding:NSUTF8StringEncoding error:nil];
写字符串到文件:writeToFile方法
NSString *string7 = [[NSString alloc] initWithString:@"This is a string7!"];
NSString *path7 = @"string7.text";
[string7 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
用C语言库函数实现比较:strcmp函数
char stringc1[] = "string!",stringc2[] = "string!";
if(strcmp(stringc1, stringc2) == 0)
NSLog(@"yes");
oc中使用NSString:isEqualToString方法
NSString *astringoc1 = @"This is a String!";
NSString *astringoc2 = @"This is a String!";
BOOL result = [astringoc1 isEqualToString:astringoc2];
NSLog(@"result:%d",result);
compare方法(comparer返回的三种值)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result1 = [astring01 compare:astring02] == NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedSame 判断两者内容是否相同
BOOL result2 = [astring01 compare:astring02]==NSOrderedAscending;
NSLog(@"result:%d",result2);
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
BOOL result3 = [astring01 compare:astring02] == NSOrderedDescending;
NSLog(@"result:%d",result3);
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
不考虑大小写比较字符串(caseInsensitiveCompare)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame;
NSLog(@"result:%d",result);
(NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。)
改变字符串的大小写( uppercaseString,lowercaseString,capitalizedString)
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大写
NSLog(@"string2:%@",[string2 lowercaseString]);//小写
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大写
在串中搜索子串(rangeOfString)
NSString *string1 = @"This is a string string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
NSInteger location = range.location;
NSInteger leight = range.length;
//range 中包含了 location (出现的位置) 和length (长度)的属性
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%ld,Leight:%ld",location,leight]];
NSLog(@"astring:%@",astring);
抽取子串 (substringToIndex,substringFromIndex)
//-substringToIndex: 从字符串的开头一直截取到指定的位置,下标从零开始
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
扩展路径
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
文件扩展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
2、NSMutableString 的用法
给字符串分配容量( stringWithCapacity)
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];
在已有字符串后面添加字符(appendString,appendFormat)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 appendString:@", I will be adding some character"];
在已有字符串中按照所给出范围和长度删除字符 (deleteCharactersInRange)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
在已有字符串后面在所指定的位置中插入给出的字符串(-insertString: atIndex)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
将已有的非空符串换成其它的字符串(-setString:)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
按照所给出的范围,和字符串替换的原有的字符(replaceCharactersInRange)
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
判断字符串内是否还包含别的字符串(前缀,后缀)
//01: 检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString,hasSuffix以什么字符串结尾
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] == 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] == 1 ? NSLog(@"YES") : NSLog(@"NO");
//02: 查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过;
来源:oschina
链接:https://my.oschina.net/u/1245365/blog/172250