//~~~~~~~~创建一个字符串对象
//1.创建常量字符串
NSString *str1 = @"Don't worry, be happy";
//2.创建空字符串 赋值
NSString *str2 = [[NSString alloc] init];
str2 = @"I LIKE DOTA";
//3.initwithstring
//4.
NSString *str3 = [[NSString alloc] initWithFormat:@"I LIKE DOTA2"];
NSString *str4 = [[NSString alloc] initWithFormat:@"I LIKE DOTA%d", 2];
//~~~~~~~~字符串转化
NSString *str5 = @"hello";
char *p = [str5 UTF8String];//将oc字符串转化为c的字符串
int a = [str5 intValue];//讲数字串转化为整形数据
[str5 floatValue];//转为float类型
[str5 doubleValue];//double
//常用的NSString Message
NSLog(@"%ld", [str1 length]);//求字符串长度
NSLog(@"%c", [str1 characterAtIndex:1]);//获取字符串中的字符
BOOL r = [str1 isEqualToString:str2];//比较两个字符串是否相等,相等返回YES(1),不相等返回NO(0) (BOOL YES(1) NO(0))
long r1 = [str1 compare:str2];//比较两个字符串大小, str1大于 返回1 相等0 小于 -1
long r2 = [str1 caseInsensitiveCompare:str2];//不区分大写比较
NSString *str11 = [str1 uppercaseString];//将所有小写改成大写 不改变原来的字符串
str11 = [str2 lowercaseString];//大写改小写 ..
NSLog(@"%@", str11);
str11 = [str11 capitalizedString];// 将字符串首字母大写
NSLog(@"%@", str11);
NSRange range = [str1 rangeOfString:@"be"];//查找子串,找不到返回NSNotFound 找到返回location和length
if (range.location != NSNotFound) {
NSLog(@"%ld %ld",range.location, range.length);
}
NSString *p1 = [str1 substringToIndex:5];//字符串抽取,从小标0开始到5 不包括5
NSString *p2 = [str1 substringFromIndex:5];//从5开始往后到字符串结束,包括5
NSString *p3 = [str1 substringWithRange:range];//在range的范围里抽取
NSString *p4 = [str1 substringWithRange:NSMakeRange(2, 3)];//NSMakeRange可以生成一个range结构体
BOOL r3 = [str1 hasPrefix:@"D"];//判断前缀
BOOL r4 = [str1 hasSuffix:@"A"];//判断后缀
//NSMutableArray可变字符串 继承NSString可以使用NSString的所有方法
NSMutableString *strr = [[NSMutableString alloc] initWithString:@"i like dota2"];
[strr insertString:@"萌萌哒" atIndex:1];//在制定位置插入NSString类型字符串
[strr appendString:@"very very like"];//拼接字符串
[strr deleteCharactersInRange:NSMakeRange(1, 3)];//从指定下标删除length个字符串
[strr setString:@"没有时间打dota了"];//修改字符串
[strr replaceCharactersInRange:NSMakeRange(5, 4) withString:@"游戏"];//将指定下标位置的length个字符替换为指定的字符串
NSLog(@"%@", strr);