//创建一个字符串常量
NSString *string0 = @"asd";
//NSMutableString *mstring = @"qwe";
NSLog(@"string0 = %@", string0);
//创建一个空的字符串
NSString *string1 = [[NSStringalloc] init];
NSString *string2 = [NSStringstring];
NSLog(@"string1 = %@", string1);
NSLog(@"string2 = %@", string2);
//快速创建一个字符串,内存在常量区
NSString *string3 = [[NSStringalloc] initWithString:@"qwe"]; //调用系统的方法时,如果是实例方法就是 init 开头, 如 initWithString
NSString *string4 = [NSStringstringWithString:@"qwe"]; //调用系统方法时,如果是类方法就是以方法的参数类型开头 ,如stringWithString
NSLog(@"string3 = %@", string3);
NSLog(@"string4 = %@", string4);
//创建一个格式化字符串, 内存在堆区
NSString *string5 = [[NSStringalloc] initWithFormat:@"%d",10];
NSString *string6 = [NSStringstringWithFormat:@"%f", 10.8];
NSString *string7 = [NSStringstringWithFormat:@"%f", 10.8];
NSLog(@"string5 = %@", string5);
NSLog(@"string6 = %@", string6);
NSString *string8 = [NSStringstringWithFormat:@"qwe"];
NSString *string9 = [NSStringstringWithFormat:@"qwe"];
//比较字符串的内容
if ([string3 isEqualToString:string4]) {
NSLog(@"一样的.1");
}
else{
NSLog(@"不一样的.1");
}
//比较是否为同一对象.必须是相同类型的方法相比较。
if (string6 == string7) {
NSLog(@"一样的.2");
}
else{
NSLog(@"不一样的.2");
}
//比较字符串的大小。0则相等, -1则在Ascil码表中前者数小,1则表示前者数大,
NSString *string10 = [[NSStringalloc] initWithFormat:@"e"];
NSString *string11 = [[NSStringalloc] initWithFormat:@"aLl"];
NSLog(@"%ld", [string10caseInsensitiveCompare:string11]);
//求字符串的长度
NSLog(@"%d", [string11length]);
//全部大写
NSLog(@"%@",[string11uppercaseString]);
//全部小写
NSLog(@"%@", [string11lowercaseString]);
//首字母大写
NSLog(@"%@", [string11capitalizedString]);
//将字符串转化为基本数据类型
NSString *string12 = @"3.14";
NSLog(@"%d", [string12boolValue]);
NSLog(@"%f", [string12floatValue]);
NSLog(@"%f", [string12doubleValue]);
NSLog(@"%d", [string12intValue]);
//将字符串转化为数组
NSString *string13 = @"this is array";
NSArray *array = [string13 componentsSeparatedByString:@" "];
NSLog(@"arry = %@", array);
//截取字符串
NSString *string15 = @"abcdefg";
NSString *substring0 = [string15substringToIndex:2]; //截取前面两个
NSString *substring1 = [string15substringFromIndex:2]; //从第二个开始截取
NSLog(@"sub0 = %@", substring0);
NSLog(@"sub1 = %@", substring1);
NSRange range;
range.length = 2; //长度是多少。这不是点语法,而是访问结构体
range.location = 2; //从第几位开始
NSString *substring2 = [string15substringWithRange:range]; //从什么地方截取多少位
NSLog(@"sub2 = %@", substring2);
//字符串的拼接
NSString *string16 = @"ab";
NSString *string17 = @"cd";
NSString *string1617 = [[NSStringalloc] initWithFormat:@"%@%@", string16, string17];
NSString *str0 = [string16 stringByAppendingFormat:@"%@", string17];
NSString *str1 = [string16 stringByAppendingString:string17];
NSLog(@"%@", string1617);
NSLog(@"%@", str0);
NSLog(@"%@", str1);
//字符串的查询
NSString *link =@"asdfghjkl = _asdfghjkl";
NSRange range1 = [link rangeOfString:@"jkl = _asd"];
NSLog(@"%@",NSStringFromRange(range1));
if (range1.location !=NSNotFound) {
NSLog(@"found");
}
/*----------------------可变字符串--------------------*/
//插入字符串
NSMutableString *mutablestring1 = [[NSMutableStringalloc] initWithFormat:@"ad"];
[mutablestring1 insertString:@"bc"atIndex:1]; //插入什么字符,在那个位置插入,从0开始算
NSLog(@"mu %@", mutablestring1);
//替换
[mutablestring1 replaceCharactersInRange:NSMakeRange(0,4)withString:@"qw"]; //从那开始,替换的范围几个字符。要替换成的字符是什么。
NSLog(@"mu %@", mutablestring1)
//删除
[mutablestring1 deleteCharactersInRange:NSMakeRange(0,1)]; //从那开始删除,开始删除几个字符。
NSLog(@"mu %@", mutablestring1);