/1、创建常量字符串。
NSString *astring = @"Welcome to 1000phone";
//2、通过实例化方法 initWithString:实例化一个字符串对象
NSString *astring = [[NSString alloc] initWithString:@" I love iOS!"];NSLog(@"astring:%@",astring);
[astring release];
//3、用标准c创建字符串: initWithCString:encoding:方法
const char *cString = "I love iphone";
NSString * aString = [[NSString alloc]initWithCString:cStringencoding:NSUTF8StringEncoding];
NSLog(@"astring:%@",aString);
[aString release];
或者:用 initWithUTF8String:实例化一个字符串对象const char *p = " Welcome to Beijing!";
NSString *string = [[NSString alloc]initWithUTF8String:p];
//4、创建格式化字符串:占位符(由一个%加一个字符组成)
int age = 23;
NSString *astring = [[NSString alloc] initWithFormat:@”I am %d”,age]];NSLog(@"astring:%@",astring);
[astring release];
//5、通过静态方法创建字符串对象
NSString * str1 = [NSString stringWithString:@"I love programming!"];
NSString * str2 = [NSString stringWithUTF8String:" I love programming!"];NSString * str3 = [NSString stringWithFormat:@"%@",@" I love programming!"];
//6. 从文件读取字符串:initWithContentsOfFile:
NSString *path = @" /Users/qianfeng/Desktop/StringAPI.pdf"; //文件路径NSString *astring = [[NSString alloc] initWithContentsOfFile:path];NSLog(@"astring:%@",astring);
[astring release];
/*----------------比较两个字符串----------------*///用 C 比较:strcmp 函数
char string1[] = "string!";
char string2[] = "string!";if(strcmp(string1, string2) = = 0){
NSLog(@"1");}
//1. isEqualToString 方法 判断两个字符串是否相等 相等返回 1 不等返回 0
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];NSLog(@"result:%d",result);
//2. compare 方法(compare:返回的三种值 分别是-1,0,1)
//NSOrderedSame 判断两者内容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;//0NSLog(@"result:%d",result);
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring01小于 astring02 为真)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;NSLog(@"result:%d",result); //
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring01大于 astring02 为真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;NSLog(@"result:%d",result);
//3. 不考虑大小写比较字符串
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = =NSOrderedSame;
NSLog(@"result:%d",result);
enum _NSComparisonResult {
NSOrderedAscending = -1, // astring01 小于 astring02NSOrderedSame, astring01 等于 astring02NSOrderedDescending astring01 大于 astring02};
/*----------------改变字符串的大小写----------------*/NSString *string1 = @"A String";
NSString *string2 = @"String";
//1. uppercaseString 小写转大写 string1 指向的对象内容不会发生改变(会产生一个新的字符串对象,string 指向这个新的对象)
NSString *string = [string1 uppercaseString];
NSLog(@"string:%@",string);
//2. lowercaseString 大写转小写string = [string1 lowercaseString];NSLog(@"string:%@",string);
//3. capitalizedString 单词首字母大写 其余小写,其他字符保持不变NSLog(@"string:%@",string);//首字母大小
/*----------------搜索子串----------------*/
NSString *string1 = @"I love iOS very much!";
NSString *string2 = @"iOS";
NSRange range = [string1 rangeOfString:string2]; //NSRange 代表一个范围:子串 string2 在长串 string1 中的范围(位置和长度)
NSUInteger location=range.location;
NSUInteger length= range.length;
NSString *astring = [NSStringstringWithFormat:@"Location:%lu,Leight:%lu",location, length];NSLog(@"astring:%@",astring);
[astring release];
/*----------------抽取子串 ----------------*/
//1. -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位
置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];NSLog(@"string2:%@",string2);
//2. -substringFromIndex: 从指定位置开始(包括指定位置的字符)提取,并包括之后的全部字符 直到最后;
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];NSLog(@"string2:%@",string2);
//3. -substringWithRange: //在指定范围内从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];NSLog(@"string2:%@",string2);
/*------------判断字符串内是否还包含别的字符串(前缀,后缀)------------*///01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *)
aString; (判断前缀)
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");//02:检查字符串是否以另一个字符串结尾- (BOOL) hasSuffix: (NSString *)aString; (判断后缀)
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
/******************************************************************************
二.NSMutableString*******************************************************************************/
/*---------------给字符串分配预设空间大小----------------*///stringWithCapacity:
NSMutableString *string;
string = [NSMutableString stringWithCapacity:20];//预设一个 20 字节的字符串空间
string.string = @”Welcome to qianfeng”;
/*---------------增加字符串----------------*/
//1. appendString: appendFormat: 追加,在后面追加字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
//[String1 appendString:@", I will be adding some characters"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding somecharacters"]];
NSLog(@"String1:%@",String1);
//2. -insertString: atIndex: 在指定位置插入字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
/*--------在已有字符串中按照所给出范围和长度删除字符------*/
//deleteCharactersInRange: 在指定范围内删除字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a
NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];NSLog(@"String1:%@",String1);
/*--------修改字符串------*///-setString: 修改字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
/*--------在指定范围内,替换的原有的字符------*/// replaceCharactersInRange: withString: 替换
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];NSLog(@"String1:%@",String1);