字符串操作:
-(void) testString{
NSString *str1 = @"some string";
NSLog(@"%@", str1); //格式化字符串
NSString *str2 = [NSString stringWithFormat:@"%@ %@", @"hello", @];
NSLog(@"%@", str2); //获取以数字开头的字符串的数字
NSString *str3 = @“.3abc12";
double temp1 = [str3 floatValue];
NSInteger temp2 = [str3 integerValue];
NSLog(@"%f", temp1);
NSLog(@"%lu", temp2); //获取字符串长度
NSUInteger len = [str3 length];
NSLog(@"%lu", len); //比较字符串
NSString *str4 = @“.3abC12";
BOOL isEqual = [str3 isEqualToString:str4];
NSLog(@"%hhd", isEqual);
//比较字符串(不区分大小写)
isEqual = ([str3 caseInsensitiveCompare:str4] == NSOrderedSame);
NSLog(@"%ld", (long)[str3 caseInsensitiveCompare:str4]);
NSLog(@"%hhd", isEqual); //将字符串全部字符转换为大写字符
str4 = [str4 uppercaseString];
NSLog(@"%@", str4);
//将字符串全部字符转换为小写字符 str4 = [str4 lowercaseString];
NSLog(@"%@", str4); //去除字符串空字符
NSString *str5 = @" one two three ";
//去除字符串空字符,包括前后空格,换行符
str1 = [str5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//去除字符串空字符,包括前后空格 str2 = [str5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"%@",str1);
NSLog(@"%@",str2); //获得substring
str3 = [str1 substringToIndex:];
str4 = [NSString stringWithFormat:@"%@%@",[str2 substringFromIndex:], @"" ];
NSLog(@"%@", str3);
NSLog(@"%@", str4);
//分割字符串
NSArray *arr = [str1 componentsSeparatedByString:@" "];
NSLog(@"%@", arr);
}
对应输出结果:
-- ::02.607 test2[:] some string
-- ::02.607 test2[:] hello
-- ::02.608 test2[:] 124.300003
-- ::02.608 test2[:]
-- ::02.608 test2[:]
-- ::02.608 test2[:]
-- ::02.608 test2[:]
-- ::02.608 test2[:]
-- ::02.608 test2[:] .3ABC12
-- ::02.608 test2[:] .3abc12
-- ::02.608 test2[:] one two three
-- ::02.608 test2[:] one two three
-- ::02.608 test2[:] one
-- ::02.608 test2[:] two three111
-- ::02.608 test2[:] (
one,
two,
three
)