NSString NSMutableString

-------------------------------------------------------------------------NSString-------------------------------------------------------------------------------

1:NSData 转换成 NSString

NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];

2:获取NSString 扩展后缀 (abc.txt) 如果没有后缀,返回的字符串长度为0

NSString *strFileExtension = [strFilePath pathExtension];

3:利用NSRange 进行字符串截取

NSRange range = NSMakeRange(1, 2);
NSLog(@"%@",[@"余书懿" substringWithRange:range]); 

输出:书懿

还可以从头开发截取,或者从末尾开始截取

NSLog(@"%@",NSTemporaryDirectory());
NSLog(@"%@",[NSTemporaryDirectory() substringFromIndex:2]); //
NSLog(@"%@",[NSTemporaryDirectory() substringToIndex:2]);

输出:

2012-11-07 09:51:58.020 FEMicroCoop[1558:c07] /Users/amarishuyi/Library/Application Support/iPhone Simulator/6.0/Applications/1795A32F-605D-4C10-8903-A70668048C76/tmp/

2012-11-07 09:51:59.257 FEMicroCoop[1558:c07]/Users/amarishuyi/Library/Application Support/iPhone Simulator/6.0/Applications/1795A32F-605D-4C10-8903-A70668048C76/tmp/

2012-11-07 09:52:13.126 FEMicroCoop[1558:c07] /Users/amarishuyi/Library/Application Support/iPhone Simulator/6.0/Applications/1795A32F-605D-4C10-8903-A70668048C76/tmp/


注:红色表示被截取掉的.



4:大小写转换

[@"abc" uppercaseString]; //大写
[@"ABC" lowercaseString]; //小写

5:字符串编码格式转换:

转换成Url 可以接受的格式

self.attachmentUrlString = [self.attachmentUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

转换回来

NSString *encodeFilePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


6:判断NSString 是否包含中文

NSString *str = @"i'm a 苹果。...";
for(int i=0; i< [str length];i++){
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff)
NSLog(@"汉字");
}

7:根据字符串长度计算所占用的实际空间

NSLog(@"%@",NSStringFromCGSize([testString sizeWithFont:[UIFont systemFontOfSize:10]]));
这里根据字体设定的大小来确定CGSize


subtitleSize = [subtitleText sizeWithFont:[UIFont systemFontOfSize:12]
                                constrainedToSize:CGSizeMake(180.0, 4000)
                                    lineBreakMode:UILineBreakModeWordWrap];
这个用来确认每一行以180像素的宽度,然后自动换行UILineBreakModeWordWrap需要用多少占用面积.多用在计算每一列UITableViewCell的高度


8:去掉字符串两段空格和回车

searchResult = [searchResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];


9:将字符串分割为数组   

NSArray *aImageNameInfo = [aImageName componentsSeparatedByString:@"_"];



-------------------------------------------------------------------------NSString-------------------------------------------------------------------------------

-------------------------------------------------------------------------NSMutableString-------------------------------------------------------------------------------


1:NSMutableString 字符串替换

NSMutableString *muStrHTML = [NSMutableString stringWithString:_detailsItem.html];
NSRange range = NSMakeRange(0, [muStrHTML length]); 
[muStrHTML replaceOccurrencesOfString:@"a" withString:@"b" options:NSCaseInsensitiveSearch range:range];


NSCaseInsensitiveSearch 表示不区分大小写进行替换

-------------------------------------------------------------------------NSMutableString-------------------------------------------------------------------------------






上一篇:NSBundle UIScreen


下一篇:Painter绘制教程:绘制超写实萌男孩小正太