标签:nsstring ios开发 objective-c 遍历
比较内容BOOL isEqual=【string1 isEqualToString:String2];
比较指针地址(string1==string2)
比较字符串大小 升降序
NSComparisonResult result =[string10 caseInsensitiveCompare:string11];
求字符串长度
NSlog(@“%d”,[string11 length]);
字符串转换
NSLog(@“upper:%@“,[string11 uppercaseString]);大写
NSLog(@“lower:%@“,[string11 lowerCaseString]);小写
NSLog(@“capital:%@“,[string11 capitalizedString]);首字母大写
转换基本数据类型
NSString *string13=@“3,14”;
float ppi=[string13 floatValue];
字符串转换成数组
NSString* string14 =@“abc bcd xyz”;
NSArray*array=[string14 componentsSeparatedByString:@“ ”];(引号内是以空格分割)
NSLog(@“array:%@”,array);
截取字符串
NSString*string1=[string15 substringTOIndex:2];不包含第二位(到那位)
NSString*string2=[string15 substringFromIndex:2:];包含第二位(从那位开始)
NSRange range={4,2};
NSString *subString=[string15 substringWithRange:range];
//字符串的拼接
NSString*appString2=[string16 stringByAppendingFormat:@“%@“,string17];
NSString*appString2=[string16 stringByAppendingString:string17];
查找字符串
NSString *link=@“abcdefgh=_blankxyz”
NSRange range=[link rangeOfString;@“fgh=_blank”];
if(range1.location !=NSNotFound){ //(NSNotFound=NSIntegerMax)
NSLog(@“founded");
}
可变字符串
MSMutableString *mutableString=[[NSMutableString alloc] initWithFormat:@“abc”]
增
[mutableString1 insertString:@“xyz”atIndex:3];
改
[mutableString replaceCharactersInRang:NSMakeRange(0,3)withString:@“efg"];
删
[mutableString1 deleteCharactersInRange:NSMakeRange(0,3)];
计算text字符串的size
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
OC遍历字符串
1) 通过查找的方式来(这方式适合所有格式的子符串,推荐使用)
NSString *newStr =@"abdcdddccdd00大家好哦";
NSString *temp = nil;
for(int i =0; i < [newStr length]; i++)
{
temp = [newStr substringWithRange:NSMakeRange(i, 1)];
NSLog(@"第%d个字是:%@",i,temp);
}
(2) 通过遍历字符的方式遍历字符串(只适合不包含中文的字符串)
NSString *newStr = @"abdcdddccdd00";
for(int i =0; i < [newStr length]; i++)
{
NSLog(@"第%d个字符是:%@",i, [newStr characterAtIndex:i]);
}
Foundation框架--NSString类字符串常用操作
标签:nsstring ios开发 objective-c 遍历
原文地址:http://blog.csdn.net/u010804473/article/details/45221147