NSstring * str1 = @"hello";
NSString * str = [[NSString alloc]initWithString:@"hello world!"];
NSString * str_1 = [[NSString alloc]initWithUTF8String:"hello world"];//把C的字符串转化为OC的字符串
int a = 123;
NSString * str_3 = [[NSString alloc]initWithFormat:@"a = %d %s%@", a, "abcd", @"efg"];//用格式化字符串初始化
可完成字符串的拼接以及完成C的字符串与OC的字符串的转化
NSString * str1 = [NSString stringWithString:@"hello world"];
NSString * str2 = [NSString stringWithUTF8String:"hello world"];
NSString * str3 = [NSString stringWithFormat:@"%s","hello world"];
NSString * str5 = @"helloworld";
NSString * ptr1 = [str5 substringToIndex:4];//字符串抽取 从下标0开始到4 不包括4
NSString * ptr2 = [str5 substringFromIndex:5];//从下标5开始抽取到字符串结束 包括5
NSRange range1 = {4,3};//结构体初始化
NSString * ptr3 = [str5 substringWithRange:range1];//在range指定范围内抽取
NSString *ptr4=[str5 substringWithRange:NSMakeRange(4,3)];//NSMakeRange可以生成一个结构体
NSString * str = @"www.1000phone.com";
BOOL ret = [str hasPrefix:@"www"];//判断字符串是否以指定字符串开头
NSString * str1 = @"1.txt";
BOOL ret1 = [str hasSuffix:@".txt"];//判断字符串是否以指定的字符串结尾
NSMutableString可变字符串(动态增加和减少的) 继承于NSString可以使用NSString的所有方法
NSMutableString * str = [[NSMutableString alloc]initWithString:@"hello"];//将不可变的字符串转换为可变的字符串
[str insertString:@"123" atIndex:1];//在指定下标(不要越界)位置插入NSString类型字符串
[str appendString:@"123"];//在字符串末尾追加字符串
[str deleteCharactersInRange:NSMakeRange(0, 2)];//从指定下标删除length个字符
[str setString:@"qianfeng"];//修改字符串亦称对该可变字符串赋值
[str replaceCharactersInRange:NSMakeRange(3, 1) withString:@"ios"];//将指定下标位置的length个字符替换为指定的字符串
网友评论