创建
基本:NSString *astring = @"This is a String!";
格式化:NSString *astring = [NSString stringWithFormat:@"格式化",字符串];
NSString *str = @"123456789";
NSString *str1 = @"987456321";
增加:
(1)基本添加
NSString *astring = [str stringByAppendingString:@"qq"]; // 123456789qq
(2)格式化添加
NSString *astring = [str stringByAppendingFormat:@"#%@-%@",@"A",@"B"]; // 123456789#A-B
删除:
修改:
1. 截取
(1)字符串截取
NSString *astring = [str substringFromIndex:1]; //23456789
NSString *astring = [str substringToIndex:1]; //1
NSString *astring = [str substringWithRange:NSMakeRange(1,2)]; //23
(2)字符截取
char ch = [str characterAtIndex:2]//3 长度类型是MUInteger
2.替换
NSString *astring = [str stringByReplcingOccurencesOfString:@"23" withString:@"99"];//199456789
3.改类型
int a = [str intValue];
float a = [str floatValue];
4.大小写控制
NSString *astring = [string1 uppercaseString];//大写
NSString *astring = [string2 lowercaseString];//小写
NSString *astring = [string2 capitalizedString];//首字母大小
查找:
(1)查两个字符串的大小
int a = [str compare:str1];
int a = [str compare:str1 options:参数1|参数2|参数3];
NSCaseInsensitiveSearch:不区分大小写
NSLiteralSearch:区分大小写
NSNumericSearch:比较两个字符串的字符个数
(2)比较两个字符串是否相等
BOOL bo = [str isEquralToString:str1];
(3)前缀,后缀查找
BOOL bo = [str hasPrefix:str1];
BOOL bo = [str hasSuffix:str1];
(4)中间查找
NSRange range = [str rangeOfString:str2]; //判断有没有找到:range.location == NSNotFound
写入文件
BOOL bo = [str writeToFile:filepath atnomic:YES/NO encoding:1 error:nil];
从文件中读取
NSString *astring = [NSString stringWithContentsOfFile:filepath encoding:1 error:nil];
|
|