NSString 在OC语言里面相当重要,在以后的开发中也会经常用到NSString类。
NSString为不可变字符串,其对应的可变字符串为NSMutableString。
1、NSString和NSMutableString的创建:
NSString *s1 = [NSString stringWithFormat:@”%d”,5];
NSMutableString *s2 = [NSMutableString stringWithFormat:@”djj”];
2、可变与不可变的转换
NSMutableString *s11 = [NSMutableString stringWithString:s1];
NSString *s22 = [NSString stringWithString:s2];
3、字符串操作之拼接
NSMutableString *s3 = [NSMutableString stringWithFormat:@”iPhone”];
NSString *s4 = @”6”;
[s3 appendString:s4];
4、字符串操作之截取
NSMutableString *s5 = [NSMutableString stringWithFormat:@”0123156181”];
NSRange range ;
range.location = 3;
range.length = 1;
NSString *s6 = [s5 substringWithRange:range];
5、字符串操作之替换
[s5 replaceCharactersInRange:range withString:@”x”];
6、字符串操作之查找
简单查找 NSMutableString *s8 = [NSMutableString stringWithFormat:@”0123456589”];
NSRange range1 = [s8 rangeOfString:@”5”];
NSLog(@”range1 = %@”,NSStringFromRange(range1));
高级查找--正则匹配
BOOL isA = [s8 hasPrefix:@”http://”]
BOOL isB = [s8 hasSuffix:@”.png”];
总结:以上都是字符串的一些常用的操作,必须要掌握,在开发的时候会很有帮助!!!