//NSString初始化的几种方法(3种方法)
//1.
NSString *str2 = [[NSString alloc] init];
//2.
NSString *str = @"111";
//3.
NSString *str1 = [NSString stringWithFormat:@"我叫%@,我的年龄%d",@“KZ”,23];
//字符串str1的长度
[str1 length]
//判断str1中是否包含str2这个前缀,包含返回YES,不包含返回NO
[str1 hasPrefix:str2];
//判断两个字符串是否相等,相等返回YES,不相等返回NO
[str1 isEqualToString:str2];
//取子串,通过range
NSRange range = NSMakeRange(2, 3);//(包含下标为2的值)
str2=[str1 substringWithRange:range];
//从第几个开始取子串,一直取到最后
str2=[string2 substringFromIndex:3];//(包含下标为3的值)
//这个是从第0个开始取,一直取到第5个
str2=[string2 substringToIndex:5];//(不包含下标为5的值)
//拼接字符串
str2 = [@"尚承" stringByAppendingString:@"是一个很好的培训机构"];
//通过逗号分隔一个字符串成一个数组
NSArray *array = [str2 componentsSeparatedByString:@","];
//这个是将数组转换成JSONString打印出来
NSLog(@"%@",[array JSONString]);
//字符串转换成小写,大写,首字母大写其余字母小写
NSString *str3 = @"hello world";
str2=[str3 lowercaseString];
str2=[str3 uppercaseString];
NSString *str3 = @"hello world";
str2=[str3 lowercaseString];
str2=[str3 uppercaseString];
str2=[str3 capitalizedString];
//把c语言里边的字符串指针或字符串数组转换成OC下用到的NSString
const char *a = "hello";
const char *a = "hello";
str2 = [[NSString alloc] initWithUTF8String:a];
//str1和str2比较大小
NSComparisonResult result=[str1 compare:str2];
switch (result){
case NSOrderedAscending:{ //升序
NSLog(@"<");
}break;
case NSOrderedDescending:{ //降序
NSLog(@">");
}break;
case NSOrderedSame:{ //相等
NSLog(@"=");
}break;
}break;
}
//NSString转换成int float double
//OC 苹果提倡用NSInteger 32位系统下跟int相同是4bytes 64位系统下跟long相同是8bytes
str2 = @"100.11";
NSLog(@"%d",[str2 intValue]);
NSLog(@"%f",[str2 floatValue]);
NSLog(@"%lf",[str2 doubleValue]);
/*********************************************************************/
/************************** NSMutableString **************************/
/*********************************************************************/
//NSMutableString跟NSString的区别是可以增删改查
//增加一个子串
str2=[str1 appendString:@"abc"]
//用NSString实现NSMutableString增加子串的功能
str2 = [@"" stringByAppendingString:@"abc"];
//删除一个子串
NSMutableString *str1=[NSMutableString stringWithString:@"abcde"];
NSString *str2=@"mn";
NSRange range=NSMakeRange(1,[str2 length]);
[str1 deleteCharactersInRange:range];
NSString *str2=@"mn";
NSRange range=NSMakeRange(1,[str2 length]);
[str1 deleteCharactersInRange:range];
NSLog(@"%@",str1);//str1=ade;
//用NSString实现NSMutableString删除子串的功能
NSArray *array3 = [@"abc" componentsSeparatedByString:@"b"];
NSString *string13 = @"";
for (NSString *a in array3)
{
for (NSString *a in array3)
{
string13 = [string13 stringByAppendingString:a];
}
for (int i = 0; i < [array3 count]; i++)
{
NSString *a = [array3 objectAtIndex:i];
string13 = [string13 stringByAppendingString:a];
for (int i = 0; i < [array3 count]; i++)
{
NSString *a = [array3 objectAtIndex:i];
string13 = [string13 stringByAppendingString:a];
}
//指定位置插入一个子串
[str2 insertString:@"b" atIndex:1];
//用指定字符串替换对应range的字符串
//从下标为x的位置开始,删除y个长度的字节,加上新的string
[str2 replaceCharactersInRange:NSMakeRange(x, y) withString:@"dj"];
//判断字符串@"aBcD_EfGk"中是否有EfGk,如果有将EfGk转换成WXYZ,并让字符串中的大写字母转成小写,输出修改完成的字符串
NSMutableString *string14 = [[NSMutableString alloc] initWithString:@"aBcD_EfGk"];
NSRange range14 = [string14 rangeOfString:@"EfGk"];
if (range14.length != 0)
{
[string14 replaceCharactersInRange:range14 withString:@"WXYZ"];
}
NSMutableString *string14 = [[NSMutableString alloc] initWithString:@"aBcD_EfGk"];
NSRange range14 = [string14 rangeOfString:@"EfGk"];
if (range14.length != 0)
{
[string14 replaceCharactersInRange:range14 withString:@"WXYZ"];
}
NSLog(@"%@",[string14 lowercaseString]);
return 0;
}
}