创建NSString对象
/*使用实例方法创建NSString对象*/ NSString *string1=[[NSString alloc]initWithFormat:@"学号12111"];//格式化创建字符串 NSString *string2=[[NSString alloc]initWithFormat:@"机构名称:%@ 成立时间:%d",@"中嘉博众",2001]; NSString *str1=[[NSString alloc]initWithString:string1]; NSLog(@"%@",string1); NSLog(@"%@",string2); NSLog(@"%@",str1); /*使用类方法创建NSString对象*/ NSString *string3=[NSString stringWithFormat:@"姓名:%@ 年龄:%d",@"小明",20]; NSLog(@"%@",string3);
其他用法
//计算字符串长度 NSUInteger strlen=[str length]; NSLog(@"%ld",strlen);
//比较字符串对象是否相同(1) if([str isEqualToString:@"创建一个字符串常亮da"]){ NSLog(@"两个字符串相等"); }else{ NSLog(@"两个字符串不相等"); } //(2) if(str==str0){ NSLog(@"两个字符串相等"); }else{ NSLog(@"两个字符串不相等"); }
//两个字符串比较(逐个相比)比较大小 NSComparisonResult result=[str compare:str3]; // NSComparisonResult result1=[str caseInsensitiveCompare:str3];比较字符串 NSLog(@"%ld",result);//返回-1则第二个数大 0为相等 1为第一个大 if(result==NSOrderedAscending){ NSLog(@"str<str3"); }else if (result==NSOrderedDescending){ NSLog(@"str>str3"); }else{ NSLog(@"str=str3"); }
/*将对象类型转变为基础类型输出*/ float num=24.32; NSString *str6=[NSString stringWithFormat:@"%f",num]; int num1=[str6 intValue]; //强制转换 NSLog(@"%d",num1); NSString *str7=@"http://localhost:8080/test/user=admin&pwd=123"; const char *str8=[str7 UTF8String]; //强制转换 用%s输出 NSLog(@"%s",str8); NSLog(@"%@",str7);
/*判断字符串是否有指定的前缀*/ BOOL result1=[string1 hasPrefix:@"学号"]; NSLog(@"%d",result1); if([string1 hasPrefix:@"学号"]){ NSLog(@"yes"); }else{ NSLog(@"no"); }
/*判断字符串是否有指定的后缀*/ BOOL result2=[string1 hasSuffix:@"1"]; NSLog(@"%d",result2); if([string1 hasSuffix:@"1"]){ NSLog(@"yes"); }else{ NSLog(@"no"); }
/*分割字符串*/ NSString *str9=@"哈哈 你呗拍类"; NSString *str10=[str9 substringFromIndex:5]; //从第五个字符开始截取 NSString *str11=[str9 substringToIndex:4]; //截取到第四个字符 NSLog(@"%@",str10); NSLog(@"%@",str11); NSRange rang2=NSMakeRange(5,2); //从第五位开始截取两位 NSString *str12=[str9 substringWithRange:rang2]; NSLog(@"%@",str12);
/*拼接字符串*/ NSString *str13=@"hello"; NSString *str14=[str13 stringByAppendingFormat:@" world"]; NSLog(@"%@",str14);
/*大小写转换*/ NSString *str15=@"hello world"; NSLog(@"大写:%@",[str15 uppercaseString]); NSLog(@"小写:%@",[str15 lowercaseString]); NSLog(@"首字母大写:%@",[str15 capitalizedString]);
/*查找字符串的位置*/ NSRange rang1=[str7 rangeOfString:@"http"]; if(rang1.location==NSNotFound){ NSLog(@"未找到"); }else{ NSLog(@"%@",NSStringFromRange(rang1));//快速输出范围 NSLog(@"location:%ld length:%ld",rang1.location,rang1.length); }
// 替换 NSString *string6 = [string2 stringByReplacingOccurrencesOfString:@"蓝鸥" withString:@"海鸥"]; NSLog(@"%@", string6); NSRange range3 = NSMakeRange(3, 3); NSString *string7 = [string2 stringByReplacingCharactersInRange:range3 withString:@"HAHA 你中招了"]; NSLog(@"%@", string7);
1 /*截取字符串 从网址中截取用户名和密码*/ 2 NSString *str=@"http://localhost:8090.cn?user=adminator&pwd=1234ABCD"; 3 NSRange range1=[str rangeOfString:@"user="]; 4 NSRange range2=[str rangeOfString:@"&pwd="]; 5 NSUInteger userlocation=range1.location+range1.length; 6 NSUInteger userlength=range2.location-(range1.location+range1.length); 7 NSRange userRange={userlocation,userlength};// 自定义一个结构体范围 8 NSString *str1=[str substringWithRange:userRange]; 9 NSLog(@"user=%@",str1); 10 NSUInteger strlen=[str length]; 11 NSUInteger pwdlocation=userlength+userlocation+range2.length; 12 NSUInteger pwdlength=strlen-(userlength+userlocation+range2.length); 13 NSRange pwdRange={pwdlocation,pwdlength};// 自定义一个结构体范围 14 NSString *str2=[str substringWithRange:pwdRange]; 15 NSLog(@"pwd=%@",str2);
创建可变的字符串
1 //nsstring父类 可变字符串时子类 2 //创建可变字符串 3 NSMutableString *mustr1=[[NSMutableString alloc]initWithFormat:@"Hello"]; 4 NSString *str=@"welcome to oc"; 5 //将不可变的字符串放在可变的字符串中 6 mustr1=[NSMutableString stringWithString:str]; 7 NSLog(@"%@",mustr1); 8 // 插入*(在指定位置进行插入) 9 [mustr1 insertString:@" students" atIndex:7]; 10 NSLog(@"%@",mustr1); 11 // 在末尾进行插入 12 [mustr1 appendString:@" teacher"]; 13 [mustr1 appendFormat:@" %@",str]; 14 NSLog(@"%@",mustr1); 15 //删除 16 [mustr1 deleteCharactersInRange:NSMakeRange(8,9)]; 17 NSLog(@"%@",mustr1); 18 //查找并删除 19 NSRange rang=[mustr1 rangeOfString:@"teacher welcome to oc"]; 20 if(rang.location!=NSNotFound){ 21 [mustr1 deleteCharactersInRange:rang]; 22 } 23 NSLog(@"%@",mustr1); 24 //重新赋值 25 [mustr1 setString:str]; 26 NSLog(@"%@",mustr1); 27 //替换 28 NSRange rang1=[mustr1 rangeOfString:@"oc"]; 29 [mustr1 replaceCharactersInRange:rang1 withString:@"IOS"]; 30 NSLog(@"%@",mustr1);