一、NSString
1、创建常量字符串。
NSString *string = @"hello world";
2、通过实例化方法 initWithString:实例化一个字符串对象
NSString *string = [[NSString alloc] initWithString:@"hello world"];
3、用标准c创建字符串: initWithCString:encoding:方法
const char *cString = "hello world";
NSString * String = [[NSString alloc]initWithCString:cString encoding:NSUTF8StringEncoding];
或者:用 initWithUTF8String:实例化一个字符串对象
const char *p = "hello world";
NSString *string = [[NSString alloc]initWithUTF8String:p];
4、创建格式化字符串:占位符(由一个%加一个字符组成)
int age = 23;
NSString *string = [[NSString alloc] initWithFormat:@”I am %d”,age]];
5、通过静态方法创建字符串对象
NSString * str1 = [NSString stringWithString:@"hello world"];
NSString * str2 = [NSString stringWithUTF8String:"hello world"];
NSString * str3 = [NSString stringWithFormat:@"%@",@"hello world"];
6. 从文件读取字符串:initWithContentsOfFile:
NSString *path = @" /Users/qianfeng/Desktop/StringAPI.pdf"; //文件路径
NSString *string = [[NSString alloc] initWithContentsOfFile:path];
/----------------比较两个字符串----------------/
//用 C 比较:strcmp 函数
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0) { }
1. isEqualToString 方法 判断两个字符串是否相等 相等返回 1 不等返回 0
NSString *string01 = @" hello world";
NSString *string02 = @" hello world";
BOOL result = [string01 isEqualToString:string02];
2. compare 方法(compare:返回的三种值 分别是-1,0,1)
//NSOrderedSame 判断两者内容是否相同
NSString *string01 = @" hello world";
NSString *string02 = @" hello world";
BOOL result = [string01 compare:string02] = = NSOrderedSame;//0
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,string01 小于 string02 为真)
NSString *string01 = @"hello world";
NSString *string02 = @"hello world";
BOOL result = [string01 compare:string02] = = NSOrderedAscending;
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,string01 大于 string02 为真)
NSString *string01 = @"hello world";
NSString *string02 = @"hello world";
BOOL result = [string01 compare:string02] = = NSOrderedDescending;
3. 不考虑大小写比较字符串
NSString *string01 = @"hello world";
NSString *string02 = @"hello world";
BOOL result = [string01 caseInsensitiveCompare:string02] = = NSOrderedSame;
enum _NSComparisonResult {
NSOrderedAscending = -1, // string01 小于 string02 NSOrderedSame, string01 等于 string02 NSOrderedDescending string01 大于 string02 };
/----------------改变字符串的大小写----------------/
NSString *string1 = @Hello World";
NSString *string2 = @"World";
1. uppercaseString 小写转大写 string1 指向的对象内容不会发生改变 (会产生一个新的字符串对象,string 指向这个新的对象)
NSString *string = [string1 uppercaseString];
2. lowercaseString 大写转小写
string = [string1 lowercaseString];
3. capitalizedString 单词首字母大写 其余小写,其他字符保持不变
string = [string1 capitalizedString]; //首字母大小
/----------------搜索子串----------------/
NSString *string1 = @"Hello World";
NSString *string2 = @"World";
NSRange range = [string1 rangeOfString:string2]; //NSRange 代表一个范围:子 串 string2 在长串 string1 中的范围(位置和长度)
/----------------抽取子串 ----------------/
1. -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位
置的字符
NSString *string1 = @"Hello World";
NSString *string2 = [string1 substringToIndex:3];
2. -substringFromIndex: 从指定位置开始(包括指定位置的字符)提取,并包 括之后的全部字符 直到最后;
NSString *string1 = @"Hello World";
NSString *string2 = [string1 substringFromIndex:3];
3. -substringWithRange: //在指定范围内从字符串中截取子串
NSString *string1 = @"Hello World";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
/------------判断字符串内是否还包含别的字符串(前缀,后缀)------------/
##01:检查字符串是否以另一个字符串开头
- (BOOL) hasPrefix: (NSString *);
NSString *String1 = @"Hello World";
BOOL ret = [String1 hasPrefix:@"Hello"]
##02:检查字符串是否以另一个字符串结尾- (BOOL) hasSuffix: (NSString *) aString; (判断后缀)
BOOL ret = [String1 hasSuffix:@".txt"]
二、NSMutableString
/---------------给字符串分配预设空间大小----------------/ //stringWithCapacity:
NSMutableString *string;
string = [NSMutableString stringWithCapacity:20];//预设一个 20 字节的字符串 空间
string.string = @”Hello World”;
/---------------增加字符串----------------/
1. appendString: appendFormat: 追加,在后面追加字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"Hello World"];
//[String1 appendString:@",A"];
[String1 appendFormat:[NSString stringWithFormat:@",B"]];
2. -insertString: atIndex: 在指定位置插入字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"Hello World"];
[String1 insertString:@"C " atIndex:0];
/--------在已有字符串中按照所给出范围和长度删除字符------/
//deleteCharactersInRange: 在指定范围内删除字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"
Hello World"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
/--------修改字符串------/ //-setString: 修改字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"Hello Word"];
[String1 setString:@"Hello Word!"];
/--------在指定范围内,替换的原有的字符------/ // replaceCharactersInRange: withString: 替换
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"Hello Word"];
[String1 replaceCharactersInRange:NSMakeRange(0, 5) withString:@"hello"];