NSString and NSMutableString

不可变字符串

NSString

NSLog(@”1*********************字符串的创建*********************”);//方式一:NSString *s1 = @”This is a NSString”;NSLog(@”s1: %@”,s1);*s3 = [[NSString alloc] initWithFormat:@”My age is %d”, 25];NSLog(@”s3: %@”,s3);NSLog(@”******************** 用标准C创建字符串 ****************”);// – (instancetype)initWithCString: encoding:char* cs2=”中国文字china”;NSString* newStr=[[NSString alloc]initWithCString:cs2encoding:NSUTF8StringEncoding];NSLog(@”NSString_newStr:%@”,newStr);NSLog(@”******************* C、OC类型字符串互换 ***************”);// C字符串 –> OC字符串 – (instancetype)initWithUTF8String:(const char *)bytesNSString *s4 = [[NSString alloc] initWithUTF8String:”This is a NSString!”];NSLog(@”s4: %@”,s4);*cs = [s4 UTF8String];NSLog(@”%s”,cs);NSLog(@”*************** 字符数组转换为NSString ****************”);//- (instancetype)initWithBytes: length: encoding:char b[10]=”abc238中”;NSString* tplStr=[[NSString alloc]initWithBytes:blength:10 encoding:NSUTF8StringEncoding];NSLog(@”NSString_tplStr:%@”,tplStr);NSLog(@”4*******************字符串的大小写*********************”);//uppercaseString,lowercaseString,lowercaseStringNSString *s7 = @”A String”;NSString *s8 = @”String”;NSString *s9 =@”stirng”;(@”string3首字母大写:%@”,[s9 lowercaseString]);//首字母大写NSLog(@”5***************** 比较两个字符串内容 ******************”);//- (BOOL)isEqualToString:(NSString *)aStringNSString *s0 = @”This is a String!”;NSString *s10 = @”This is a String!”;BOOL result = [s0 isEqualToString:s10];NSLog(@”result1:%d”,result);NSLog(@”6*******************在串中搜索子串*********************”);//查找字符串某处是否包含其它字符串 – (NSRange) rangeOfString: (NSString *) aString,,NSString *s11 = @”This is a string”;NSString *s12 = @”string”;NSRange range = [s11 rangeOfString:s12];NSUInteger location = range.location;NSUInteger length = range.length;NSString *s13 = [[NSString alloc] initWithString:[NSString stringWithFormat:@”location:%ld,length:%ld”,location,length]];NSLog(@”子串位置和长度为:%@”,s13);NSLog(@”7****************** 字符串截取子串 ********************”);//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符NSString *s14 = @”This is a string”;NSString *s15 = [s14 substringToIndex:9];NSLog(@”截取的字符串为:%@”,s15);//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符NSString *s16 = [s14 substringFromIndex:3];NSLog(@”截取的字符串为:%@”,s16);//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串NSString *s17 = [s14 substringWithRange:NSMakeRange(0, 4)];NSLog(@”截取的字符串为:%@”,s17);NSLog(@”8********************字符串的分割 *********************”);//- (NSArray *)componentsSeparatedByString:(NSString *)separatorNSString* source =@”零,壹,贰,叁,肆,伍,陆,柒,捌,玖”;NSArray* result1 = [source componentsSeparatedByString:@”,”];for(int i=0;i<result1.count;i++){NSLog(@”sp[%d]=%@”,i,result1[i]);}NSLog(@”***************** 去除字符串前后空格 ********************”);//- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set//+ (NSCharacterSet *)whitespaceCharacterSetNSCharacterSet* set =[NSCharacterSet whitespaceCharacterSet];NSString* source2 =@” This a blank space “;NSString* trimed=[source2 stringByTrimmingCharactersInSet:set];NSLog(@”去除空格后的结果为:[%@]”,trimed);NSLog(@”******************* 替换指定字符串 **********************”);//- (NSString *)stringByReplacingOccurrencesOfString: withString:NSString* source3=@” This a blank space “;NSString* restr=[source3 stringByReplacingOccurrencesOfString:@”This”withString:@”That”];NSLog(@”替换后的结果为:[%@]”,restr);可变字符串人之所以有一张嘴,而有两只耳朵,原因是听的要比说的多一倍。

NSString and NSMutableString

相关文章:

你感兴趣的文章:

标签云: