继续NSString,由于用的多,所以应该重点掌握
1、求字符串长度
NSString *myName = @"AOBAMA";
int length = (int)[myName length];//发现返回值类型是NSUInteger,不强转有警告,自己也不知道强转是不是常用的方法,望看到的网友能告知一下
NSLog(@"字符串的长度是:%d",length);
if ([myName length] > 5) {
NSLog(@"AAAAA");//打印AAAAA
}
2、字符串的相关转换
改变大小写
NSString *string = @"Where are You,Boy!";
//都大写
NSLog(@"%@",[string uppercaseString]);//打印WHERE ARE YOU,BOY!
//都小写
NSLog(@"%@",[string lowercaseString]);//打印where are you,boy!
//所有单词首字母大写,其它字母小写
NSLog(@"%@",[string capitalizedString]);//打印Where Are You,Boy!
将字符串转化成基本数据类型
NSString *aboutFloat = @"3.14";
NSString *aboutBool = @"NO";
NSLog(@"%f",[aboutFloat floatValue]);//打印3.140000
NSLog(@"%d",[aboutBool boolValue]);//打印0
//把float型转化为整型
NSLog(@"%d",[aboutFloat intValue]);//打印3把两个字符串合并成一个字符串
//方法一
NSString *str1 = @"Hello";
NSString *str2 = @" World";
NSString *str3 = [[NSString alloc]initWithFormat:@"%@%@",str1,str2];
NSLog(@"str1与str2合并成str3的结果为:%@",str3);//打印结果:str1与str2合并成str3的结果为:Hello World
//方法二
NSString *str4 = [str1 stringByAppendingString:str2];
NSLog(@"str1与str2合并成str4的结果为:%@",str4);//打印结果:str1与str2合并成str4的结果为:Hello World
//连接多个字符串
NSString *str5 = [str1 stringByAppendingFormat:@"%@%@%@",str1,str2,str3];
NSLog(@"str5为:%@",str5);//打印结果:str5为:HelloHello WorldHello World3、是否以一个字符串开头hasPrefix:,是否以一个字符串结尾hasSuffix
NSString *str6 = @"who are you";
BOOL isStart1 = [str6 hasPrefix:@"wh"];
BOOL isStart2 = [str6 hasPrefix:@"h"];
BOOL isEnd1 = [str6 hasSuffix:@"u"];
BOOL isEnd2 = [str6 hasSuffix:@"o"];
NSLog(@"结果:%d,,,%d,,,%d,,,%d",isStart1,isStart2,isEnd1,isEnd2);//结果:1,,,0,,,1,,,04、将字符串按规则截取成数组
NSString *str7 = @"you/have/a/baby";
NSArray *array = [str7 componentsSeparatedByString:@"/"];
NSLog(@"%@",array);//数组可以直接打印不用遍历
/*打印结果
(
you,
have,
a,
baby
)
*/5、字符串的截取
NSString *str8 = [str7 substringFromIndex:2];//从第二个位置开始街区包括2,当然以0开头
NSLog(@"%@",str8);// u/have/a/baby
NSString *str9 = [str7 substringToIndex:5];//从开始到规定的位置,但不包括该位置
NSLog(@"%@",str9);// you/h
//根据范围截取
NSRange range = NSMakeRange(2, 3);//先定义范围,也可以这样定义NSRange range = {2,3};
NSLog(@"location..%ld length...%ld",range.location,range.length);// location..2 length...3
range.location = 3;//包括2
range.length =4;//从2开始长度为3的子字符串
NSLog(@"location..%ld length...%ld",range.location,range.length);// location..3 length...4
NSString *str10 = [str7 substringWithRange:range];
NSLog(@"%@",str10);// /hav6、字符串查询
NSRange range2 = [str7 rangeOfString:@"have"];
NSRange range3 = [str7 rangeOfString:@"aaa"];
NSLog(@"location1...%ld",range2.location);
NSLog(@"location2...%ld",range3.location);打印结果:
location1...4
location2...9223372036854775807
我们可以根据要查询的字符串在源字符串中的位置来判断查找结果。
NSMutableString的相关用法
PS: 一直很困扰与Ubuntu下解压zip乱码问题,虽然解压后不影响文件的正常使用,但看着各种字符的文件名心里就各种不爽。
所以决定解决这个问题,现在将我认为比较好的方法分享给大家,希望对大家有所帮助。
其实,并不是所有zip文件解压后都是乱码的,只有windows下压缩的zip在ubuntu中会出现这种情况。
其实就是windows和ubuntu下压缩的编码格式不同。windows下的编码格式为GBK,Ubuntu下的为UTF-8。
原因分析过了,下面说一下解决办法(这些方法据说是高手们最常用的方法)
1. 安装7-zip 和 convmv :
命令: sudo apt-get install convmv p7zip-full
命令:LANG=C 7z e yourZIPfilename
3. 开始转换编码
命令: convmv -f gbk -t utf8 -r --notest *
OK,大功告成了,呵呵。。。
做应用程序,难免会用到本地文件的处理,对WP平台来说,可以采用IsolatedStorage库来实现,关于这个类库的具体操作,网上有很多的详细描述,这里就不多做描述了。当有了相应的类库后,总是使用cmd命令进行文件操作,会让人觉得非常繁琐,如果有一款GUI的工具出现,会让我们的工作变得更为简单。
我想最先遇到的工具肯定是windows phone 7 isolatedstorage explorer tool,这是一款比较老的工具,自从2011年4月份起就没有再更新过,而且界面的子目录无法进行操作,最要命的是这个工具有严重的bug。如果想让你的应用可以与PC上的GUI工具相连接,你需要在你的应用的App.xaml.cs文件内加上以下两句代码:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
IsolatedStorageExplorer.Explorer.Start("localhost");//参数为PC端的IP地址
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
IsolatedStorageExplorer.Explorer.RestoreFromTombstone();
}
如果网络出现状况,客户端无法与PC端进行连接,那么每隔几十秒,你的程序将出现崩溃。我仔细查阅过原作者在CodePlex上的主页(http://wp7explorer.codeplex.com/),在ISSUE TRACKER里,作者解释了这种频繁重启的原因,但没有提出相应的。但是网站有源码可以下载,有兴趣的朋友可以下载下源码后,将这个异常解决掉。
对于我的应用来说,由于时间较紧,并需要为用户提供一个可以通过PC机与手机应用进行数据交互的工具,那么我只能再寻找其它途径了。
幸好我找到了另外一款工具,比这个要强大很多,就是Windows Phone Power Tools(CodePlex上的主页面http://wptools.codeplex.com/),我的WP应用在win7平台下开发,此工具的最新版本需要在win8上才能运行,如果在win7上开发的话,需要下载standlone版本的安装文件。