不可变字符串:已经初始化过的字符串内容不可更改,但是可以更改字符串引用的地址
1)字符串转换成整数、单精度、双精度、NSInterger类型数据
NSString *myintstr=@"3545";
NSLog(@"mystr=%d",[myintstr intValue]);
NSString *myfloatstr=@"35.45";
NSLog(@"mystr=%.3f",[myfloatstr floatValue]);
NSLog(@"mystr=%.4f",[myfloatstr doubleValue]);
2015-05-25 16:05:17.314测试题7[3503:274398] mystr=3545
2015-05-25 16:05:17.314测试题7[3503:274398] mystr=35.450
2015-05-25 16:05:17.314测试题7[3503:274398] mystr=35.4500
2)字符串初始化
//创建一个空字符串
NSString *str11=[NSString string];//创建一个空字符串
NSLog(@"字符串:%@",str11);
//创建一个字符串并初始化为str1字符串
res =[NSString stringWithString:str1];//复制一个字符串
NSLog(@"字符串:%@",res);
//创建一个字符串并按照特定的方式初始化字符串
NSString *str12=[NSString stringWithFormat:@"nihao"];
NSLog(@"字符串:%@",str12);
NSString *str13=[NSString stringWithFormat:@"%d",1321341];
NSLog(@"字符串:%@",str13);
输出:
2015-05-25 16:34:32.735字符串练习 [3576:283221]字符串:
2015-05-25 16:34:32.735字符串练习[3576:283221]字符串:This is string A
2015-05-25 16:34:32.735字符串练习[3576:283221]字符串:nihao
2015-05-25 16:34:32.736字符串练习[3576:283221]字符串:1321341
3)对字符串字符操作
a,获取字符串中单个字符
其中
[str1 characterAtIndex:i]返回为unichar的字符
for (int i=0;i
输出:
2015-05-25 16:37:04.102字符串练习[3588:284140]第0个字符为:T
2015-05-25 16:37:04.103字符串练习[3588:284140]第1个字符为:h
2015-05-25 16:37:04.104字符串练习[3588:284140]第2个字符为:i
2015-05-25 16:37:04.104字符串练习[3588:284140]第3个字符为:s
2015-05-25 16:37:04.104字符串练习[3588:284140]第4个字符为:
2015-05-25 16:37:04.104字符串练习[3588:284140]第5个字符为:i
2015-05-25 16:37:04.104字符串练习[3588:284140]第6个字符为:s
2015-05-25 16:37:04.105字符串练习[3588:284140]第7个字符为:
2015-05-25 16:37:04.105字符串练习[3588:284140]第8个字符为:s
2015-05-25 16:37:04.105字符串练习[3588:284140]第9个字符为:t
2015-05-25 16:37:04.105字符串练习[3588:284140]第10个字符为:r
2015-05-25 16:37:04.105字符串练习[3588:284140]第11个字符为:i
2015-05-25 16:37:04.105字符串练习[3588:284140]第12个字符为:n
2015-05-25 16:37:04.106字符串练习[3588:284140]第13个字符为:g
2015-05-25 16:37:04.106字符串练习[3588:284140]第14个字符为:
2015-05-25 16:37:04.106字符串练习[3588:284140]第15个字符为:A
NSString *str20=[str1 substringToIndex:6];
NSLog(@"返回的字符串为:%@",str20);
2015-05-25 16:43:43.804字符串练习[3609:285930]返回的字符串为:This i
c),返回从第i到结尾的字符串
NSString *str21=[str1 substringFromIndex:6];
NSLog(@"返回的字符串为:%@",str21);
2015-05-25 16:46:17.150字符串练习[3620:286828]返回的字符串为:s string A
d),返回指定范围内字符串
NSMakeRange(3, 7)//指的是从索引3开始长度为7的所有对象字符
//返回指定范围内的字符串
NSString *str22=[str1 substringWithRange:NSMakeRange(3, 7)];
NSLog(@"返回的字符串为:%@",str22);
2015-05-25 16:52:27.736字符串练习[3633:288491]返回的字符串为:s is st
e),从字符串中查找部分字符串在的索引
NSRange subrange;//结构体类型
subrange=[str1 rangeOfString:@"is s"];
NSLog(@"在字符串所在位置%lu的\nis T索引是%lu\n被搜索的字符串涨肚是%lu",str1,subrange.location,subrange.length);
2015-05-25 17:38:32.219字符串练习[3746:301080]在字符串所在位置4294971464的
is T索引是5
被搜索的字符串涨肚是4
4)测试字符串
//测试字符串是否相等
NSString *str30=[NSString stringWithString:str1];
if ([str30 isEqualToString:str1]==YES)
{
NSLog(@"str30==str1");
}
else
{
NSLog(@"str30!=str1");
}
//测试字符串大小
compareResult=[str30 compare:str1];//注意该地方时compare,不是isEqualToString
if (compareResult==NSOrderedAscending)
{
NSLog(@"str30str1");
2015-05-25 17:10:50.110字符串练习[3668:293451] str30==str1
2015-05-25 17:10:50.112字符串练习[3668:293451] str30==str1
NSString *str40=[str1 uppercaseString];
NSLog(@"大写转换后:%@",str40);
NSString *str41=[str1 lowercaseString];
NSLog(@"小写转换后:%@",str41);
输出:
2015-05-25 17:15:53.350字符串练习[3680:294905]大写转换后:THIS IS STRING A
2015-05-25 17:15:53.351字符串练习[3680:294905]小写转换后:this is string a
注意:该方式不是str1字符串变更了,而是把两者追加后返回一个新的字符串
//追加一个字符串
NSString *str50=[str1 stringByAppendingString:str2 ];
NSLog(@"%@追加字符串后:%@",str1,str50);
str1=str50;
NSLog(@"%@追加字符串后:%@",str1,str50);
2015-05-25 17:23:51.720字符串练习[3696:297138]
This is string A追加字符串后:This is string AThis is string B
2015-05-25 17:23:51.721字符串练习[3696:297138]
This is string AThis is string B追加字符串后:This is string AThis is string B
可变字符串
a) 插入部分字符串
//从不可变字符串中创建可字符串
mstr=[NSMutableString stringWithString:str1];
NSLog(@"可变字符串:%@",mstr);
//向可变字符串中插入字符
[mstr insertString:@"mutable" atIndex:7];
NSLog(@"可变字符串:%@",mstr);
//向可变字符串尾部插入字符
[mstr insertString:@"last" atIndex:[mstr length]];
NSLog(@"可变字符串:%@",mstr);
//直接使用追加方法追加字符串
[mstr appendString:@" appendstring" ];
NSLog(@"可变字符串:%@",mstr);
输出:
2015-05-25 20:49:15.057 字符串练习[473:10550] 可变字符串:This is string A
2015-05-25 20:49:15.058 字符串练习[473:10550] 可变字符串:This ismutable string A
2015-05-25 20:49:15.058 字符串练习[473:10550] 可变字符串:This ismutable string Alast
2015-05-25 20:49:15.058 字符串练习[473:10550] 可变字符串:This ismutable string Alast appendstring
b),删除指定范围内字符串
-(void)deleteCharacetersInRange:range//range为指定的
NSMakeRange返回结果,其中包含索引和索引后的字符串长度
mstr=[NSMutableString stringWithString:str1];
NSLog(@"可变字符串:%@",mstr);
[mstr deleteCharactersInRange:NSMakeRange(6,3)];//删除从索引6开始以后的3个字符
NSLog(@"可变字符串:%@",mstr);
输出
2015-05-25 20:46:24.082 字符串练习[465:9635] 可变字符串:This is string A
2015-05-25 20:46:24.084 字符串练习[465:9635] 可变字符串:This itring A
mstr=[NSMutableString stringWithString:str1];
NSLog(@"可变字符串:%@",mstr);
substr =[str1 rangeOfString:@"string"];//在字符串中搜索“string”字符串
[mstr deleteCharactersInRange:substr];//删除从索引6开始以后的3个字符
NSLog(@"可变字符串:%@",mstr);
2015-05-25 20:57:48.343 字符串练习[485:12348] 可变字符串:This is string A
2015-05-25 20:57:48.345 字符串练习[485:12348] 可变字符串:This is A
mstr=[NSMutableString stringWithString:str1];
NSLog(@"可变字符串:%@",mstr);
substr =[str1 rangeOfString:@"string"];//在字符串中搜索“string”字符串
[mstr replaceCharactersInRange:substr withString:@"hello word"];
NSLog(@"可变字符串:%@",mstr);
其中字符处“string”被字符串“
hello word”替换
2015-05-25 21:03:09.081 字符串练习[504:13716] 可变字符串:This is string A
2015-05-25 21:03:09.082 字符串练习[504:13716] 可变字符串:This is hello word A
mstr=[NSMutableString stringWithString:str1];
NSLog(@"可变字符串:%@",mstr);
search=@"i";//要被搜索的字母
replace=@"I";//被替换的字母
substr =[mstr rangeOfString:@"i"];//在字符串中搜索“a”字符串
while (substr.location!=NSNotFound) {
[mstr replaceCharactersInRange:substr withString:@"I"];//替换
substr =[mstr rangeOfString:@"i"];//再次搜索
}
NSLog(@"可变字符串:%@",mstr);
2015-05-25 21:13:14.587 字符串练习[537:16093] 可变字符串:This is string A
2015-05-25 21:13:14.588 字符串练习[537:16093] 可变字符串:ThIs Is strIng A