星空网 > 软件开发 > 操作系统

OC字符串基本操作

不可变的字符串的修改方法有返回值(重新指向新的字符串地址)

可变的字符串的修改方法没有返回值(修改字符串本身)

/*

 NSString 不可变的字符串

 

 1.创建字符串对象

 */

// 使用初始化方法创建对象

NSString *string1 = [[NSString alloc] initWithFormat:@"hello, world"];

 

// 使用类方法

NSString *string2 = [NSString stringWithFormat:@"hello, world"];

 

// 使用字面量

NSString *string3 = @"hello, kitty";

 

 

 

// 2.获取字符串的长度

NSLog(@"string1length = %ld", string1.length);

NSLog(@"string1length = %ld", [string1 length]);

 

 

 

// 3.获取指定位置的字符

unichar ch = [string2 characterAtIndex:3];

NSLog(@"ch = %c", ch);

 

 

 

// 4.判断字符串的前缀和后缀

NSString *string4 = @"www.欧巴.png";

BOOL isHasSuffix = [string4 hasSuffix:@".png"];

BOOL isHasPrefix = [string4 hasPrefix:@"www."];

NSLog(@"isHasSuffix = %d, isHasPrefix = %d", isHasSuffix, isHasPrefix);

 

 

 

 

// 5.查找某一个字符串在另一个字符串中的范围

NSString *string5 = @"大风起兮云飞扬";

NSString *string6 = @"起兮";

// NSRange 一个表示范围的结构体 包含两个部分

// location表示起始位置, length表示长度

NSRange range = [string5 rangeOfString:string6];

NSLog(@"location = %ld, length = %ld", range.location, range.length);

 

 

 

 

// 6.字符串截取

NSString *string7 = @"hello world kitty";

 

// 通过指定范围截取字符串

NSRange range1 = NSMakeRange(0, 5);

NSString *subStr1 = [string7 substringWithRange:range1];

NSLog(@"%@", subStr1);

 

// 从头开始,截取一定长度

NSString *subStr2 = [string7 substringToIndex:5];

NSLog(@"%@", subStr2);

 

// 从指定位置,向后截取,包含指定位置的下标位置

NSString *subStr3 = [string7 substringFromIndex:string7.length - 5];

NSLog(@"%@", subStr3);

 

 

 

 

// 7.字符串拼接

NSString *string8 = @"nana";

NSString *string9 = @"liuaoran";

NSString *love = [string8 stringByAppendingString:string9];

//NSString *love1 = [NSString stringWithFormat:@"%@%@",string8, string9];

NSLog(@"love = %@", love);

 

 

 

 

// 8.字符串替换

NSString *string10 = @"李易峰";

NSString *string11 = [string10 stringByReplacingOccurrencesOfString:string10 withString:@"赵的住"];

NSLog(@"%@", string11);

 

 

 

 

// 9.字符串比较

NSString *string12 = @"wangcai";

NSString *string13 = @"xiaoqiang";

NSInteger result = [string12 compare:string13];

NSLog(@"%ld", result);

 

// 判断两个字符串是否相等

BOOL isEqual = [string12 isEqualToString:string13];

NSLog(@"%d", isEqual);

 

 

        

 

// 10.字符串和数值类型的转换

NSString *string14 = @"123";

NSInteger value1 = [string14 integerValue];

NSLog(@"%ld", value1);

 

 

 

 

// 11.字符串大小写之间的转换

NSString *string15 = @"da feng qi xi yun fei yang";

NSString *string16 = @"Da Feng Qi Xi Yun Fei Yang";

 

// 首字母大写

NSString *str1 = [string15 capitalizedString];

NSLog(@"%@", str1);

 

// 全部大写

NSString *str2 = [string15 uppercaseString];

NSLog(@"%@", str2);

 

// 全部小写

NSString *str3 = [string16 lowercaseString];

NSLog(@"%@", str3);

 

 

 

/**

 *  NSMutableString

 */

// 1.创建对象

// 初始化方法

NSMutableString *mStr1 = [[NSMutableString alloc] initWithFormat:@"hello, world"];

 

// 类方法

NSMutableString *mStr2 = [NSMutableString stringWithFormat:@"hello kitty"];

 

// 不能使用字面量的方式创建,因为使用字面量创建的都是不可变的字符串

 

 

 

// 2.拼接字符串

NSMutableString *mStr3 = [NSMutableString stringWithFormat:@"卫庄"];

[mStr3 appendString:@"是盖聂的师弟"];

NSLog(@"%@", mStr3);

 

 

 

// 3.插入字符串

[mStr2 insertString:@"," atIndex:5];

NSLog(@"%@", mStr2);

 

 

 

// 4.删除字符串

[mStr1 deleteCharactersInRange:NSMakeRange(5, 1)];

NSLog(@"%@", mStr1);

 




原标题:OC字符串基本操作

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

宝莲云:https://www.goluckyvip.com/tag/4429.html
虚拟海外仓怎么操作:https://www.goluckyvip.com/tag/44290.html
虚拟海外仓怎么创建:https://www.goluckyvip.com/tag/44291.html
虚拟海外仓怎么收费:https://www.goluckyvip.com/tag/44292.html
虚拟卡:https://www.goluckyvip.com/tag/44293.html
虚拟捆绑商品功能:https://www.goluckyvip.com/tag/44294.html
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流