字符串的创建:
NSString *string = [[NSString alloc] initWithFormat:@"YangSha 是女神"];
NSString *string1 =[ NSString stringWithFormat:@"YangSha 是女神"];
NSSrting *str = @"YangSha 是女神";
获取字符串中的字符:
[string characterAtIndex:index] 获取下标为index的字符,是unsigned long类型的
unichar string2 = [string charactorAtIndex :string1];
判断字符串是否相等:返回值为布尔类型
-(BOOL)isEqualToString:(NSString *)aString;
BOOL isTrue = [string1 isEqualToString:string2];
比较字符串:返回值是NSComparisonResult的类型,枚举值
-(NSComparisonResult)compare:(NSString *)string;
NSComparisonResult result = [string1 compare:string2];
/*enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;*/
从第几个index开始截取字符串:
NSString *string = [string1 substringFromIndex:5];
从头开始截取到index下标为止:
NSString *string = [string1 substringToIndex:5];
获取字符串的范围:(从下标为3之后开始取,长度为5)
NSString *string = [string substringWithRange:NSMakeRange(3,5)];
拼接字符串:
stringByAppendingFormat:
NSString *string8 = [string7 stringByAppendingString:string6];
替换字符串:(将下标3之后2个长度的字符换成别的字符)
NSString *string10 = [string5 stringByReplacingCharactersInRange:NSMakeRange(3, 2) withString:@"恭喜发财"]
字符串转换成一般的数据类型:
NSString *string = @"100";
int number = [string intValue];
字符串大写,小写,首字母大写
NSString *string13 = [string uppercaseString];
NSString *string14 = [string lowercaseString];
NSString *string15 = [string capitalizedString];
NSLog(@"%@ %@ %@",string13,string14,string15);
判断字符串是否以什么开头或者以什么结尾,返回值是布尔类型的
-(BOOL)hasPrefix:(NSString *)str;
-(BOOL)hasSuffix:(NSString *)str;
BOOL isTrue = [string hasPrefix:@"yang"];
NSLog(@"%d",isTrue);
BOOL isYes = [string hasSuffix:@"a"];
NSLog(@"%d",isYes);
NSString *string16 =@"http:www.lanou.3g.com/icon.png";
BOOL isReal = [string16 hasSuffix:@"png"];
NSLog(@"%d",isReal);
可变字符串:NSMutableString 是NSString的子类,容量给0,系统会自动申请存储空间
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:0];