NSString与NSMutableString

常用字符串函数

/*******************************************************************************

一.NSString

*******************************************************************************/

/*----------------创建字符串的方法----------------*/

//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 = [[NSStringalloc]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:返回的三种值 分别是-101)    

//NSOrderedSame判断两者内容是否相同

NSString *astring01 = @"This is a String!";    

NSString *astring02 = @"This is a String!";        

BOOL result = [astring01 compare:astring02] = = NSOrderedSame;//0

NSLog(@"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小于astring02

NSOrderedSame, astring01等于astring02

NSOrderedDescendingastring01大于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中的范围(位置和长度)

NSUIntegerlocation = range.location;    

NSUIntegerlength= range.length;    

NSString *astring = [NSString stringWithFormat:@"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 a NSMutableString"];    

//[String1 appendString:@", I will be adding some characters"];    

[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some characters"]];    

NSLog(@"String1:%@",String1);  

//2. -insertString: atIndex:    在指定位置插入字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];    

[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 a NSMutableString"];    

[String1 setString:@"Hello Word!"];    

NSLog(@"String1:%@",String1);    

/*--------在指定范围内,替换的原有的字符------*/

// replaceCharactersInRange: withString: 替换

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];    

[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];    

NSLog(@"String1:%@",String1);    


你可能感兴趣的:(NSString,NSMutableString)