C语⾔言中,字符串是由char(ASCII码)字符组成。
OC中,字符串是由unichar(Unicode)字符组成。
NSString,不可变字符串,即:创建以后,内容和⻓长度不能更改.
常见用法如下:
1.获取子字符串.
NSString *str = @"iPhone”;
NSString *s = [str substringFromIndex:2];//从下标为2的位置开始截取直到最后.(包括下标为2的字符)
NSString *s1 = [str substringToIndex:3]; //从头开始截取,但不包括下标为3的字符.
NSString *s2 = [str substringWithRange:NSMakeRange(0, 2)];//从下标为0的位置开始截取长度为2.
NSLog(@"s = %@",s);
NSLog(@"s1 = %@"ss1);
NSLog(@ss2 = %@",s2);
<pre name="code" class="objc">NSString *str = @"abc";
NSString *a = [str stringByAppendingPathComponent:@"Q”];//
NSLog(@"a = %@",a);
NSString *b = [str stringByAppendingPathExtension:@"K"];
NSLog(@"b = %@",b);
NSString *c = [str stringByAppendingString:@"R"];
NSLog(@"c =%@",c);
3.字符串替换.
NSString *str = @"iPhone"
NSString *e = [str stringByReplacingOccurrencesOfString:str withString:@"aabc"];
NSLog(@"e = %@",e);
NSString *f = [str stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:@"NK"];
NSLog(@"f = %@",f);
NSString *g = [str stringByReplacingOccurrencesOfString:str withString:@"WHY" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 3)];
NSLog(@"g = %@",g);
4.判断字符串是否以指定字符开头
[str hasPrefix:@“a”];
5.判断是否是一指定字符串结尾.
<span style="font-family:Arial, Helvetica, sans-serif;color:#333333;"><span style="font-size: 18px;">[str1 hasSuffix:@"aaa”];</span></span>
6.字符串转为int,float,he double 类型.
NSString *str = @"123”;
NSLog(@"%ld",[str intValue]);
NSLog(@“%f",[str floatValue]);
NSLog(@“%d”,[str doubleValue])