一天又一天,一日又一日,一年又会是一年。时间过得太快了,每天学习都在学习OC,可是就是觉得时间太少了,恨不得一天当48小时来过。每天都有学不完的,研究不了的事,人生总有这么多的无奈,与无奈,无奈。
下面就是今天学习的字符串的个类方法:
NSString *str = @"这是我们第一次尝试";
NSLog(@"%lu", [str length]); //获取字符串长度
NSLog(@"%lu", sizeof(unichar));
/*
NSString 字符串类常用方法
*/
//1.创建一个字符串对象
//(1)初始化方法initXXX
//NSString *erDong = @"二东"; 存在在常量区
NSString *erDong = [[NSString alloc] initWithFormat:@"二东%d%d%@", 2, 50, str]; // 存在堆
NSString *erDong2 = [[NSString alloc] initWithString:erDong]; //字符串对象
NSLog(@"erDong :%p ", erDong);
NSLog(@"erDong2 :%p ", erDong2);
NSLog(@"%@", erDong);
//(2)便利构造器
NSString *erDong13 = [NSString stringWithFormat:@"%@!!!", erDong];
NSLog(@"%@", erDong13);
NSString *shiyao = [NSString stringWithFormat:@"gafak"];
//2.判断字符串是否包含前缀(后缀)
if ([shiyao hasPrefix:@"ga"]) { //[shiyao hasPrefix:@"a"]是bool类型,只有0和1;有前缀“g” 从g开始都相同就可以,输出“有”
NSLog(@"有");
} else {
NSLog(@"没有");
}
if ([shiyao hasSuffix:@"ak"]) { //有后缀“k”,输出“有”
NSLog(@"有");
} else {
NSLog(@"没有");
}
//3.返回一个字符串在另一个字符串里的范围 没有找到length返回0;
NSRange range = [erDong13 rangeOfString:@"250"];
//查看range结构体变量的location成员变量 和 length成员变量
NSLog(@"location :%lu length: %lu", range.location, range.length);
//判断一个字符串中是否包含子串的方法
if (range.length != 0) { //可以直接写range.length
NSLog(@"找到啦!");
} else {
NSLog(@"这都输错了,学你妹的ios");
}
//4.字符串截取
NSString *caiYangZhengYu = [[NSString alloc] initWithFormat:@"蔡杨振宇"];
NSLog(@"%@", [caiYangZhengYu substringFromIndex:2]); //从下标2开始输出后面的
NSLog(@"%@",[caiYangZhengYu substringToIndex:2]); //从下标2开始输出前面的
NSRange r = {1, 2}; //从下标1开始,输出长度为2的
NSLog(@"%@", [caiYangZhengYu substringWithRange:r]); //取范围内的字符串
//5.拼接字符串 相当于创建一个新得对象,放拼接成得字符串
NSLog(@"%@", [caiYangZhengYu stringByAppendingString:@"萌萌哒"]);
NSString *caiYangZhengYuErDong3 = [[NSString alloc] initWithFormat:@"%@", erDong13];
NSLog(@"%@", caiYangZhengYuErDong3); //拼接一个对象
//NSString 拼接的真面目!!产生一个新的字符串对象
NSLog(@"蔡杨振宇 :%p", caiYangZhengYu);
NSLog(@"蔡杨振宇二东 :%p", caiYangZhengYuErDong3);
//6.替换字符串
NSRange r1 = {2, 3}; //将蔡杨振宇的下标为2的长度为3 的字符串子串给换掉。“不管传进去几个“,250就是没有了!!
NSString *a = [caiYangZhengYuErDong3 stringByReplacingCharactersInRange:r1 withString:@"07"];
NSLog(@"%@", a);
//将所有的 7 换成 “七”
NSString *string = @"12341255647679756789015678765456786524";
NSLog(@"%@", [string stringByReplacingOccurrencesOfString:@"7" withString:@"七"]);
//7.比较字符串
NSString *strA = @"A";
NSString *strB = @"B";
NSString *strC = @"C";
NSString *strBB = @"BB";
NSString *strBA = @"BA";
NSString *strA2 = @"A";
NSLog(@" A B :%ld", [strA compare:strB]);
NSLog(@" C A :%ld", [strC compare:strA]);
NSLog(@" A A2 :%ld", [strA compare:strA2]);
NSLog(@" BB BA :%ld", [strBB compare:strBA]);
NSLog(@"%ld", [@"老王" compare:@"小王"]);
NSLog(@"%ld", [@"王子龙" compare:@"孙嘉懿"]);
//8.字符串和数值类型转换
//(1)基本数据类型->字符串对象
int age = 20; //@"20"
NSString *strAge = [NSString stringWithFormat:@"%d", age];
NSLog(@"%@", strAge);
//(2)字符串对象->基本数据类型
NSLog(@"%d", [strAge intValue] + 1);
//9.大小写转换操作
NSString *caseString = @"In the compare:"
"methods, the range argument specifies the subrange"
"rather than the whole, of the receiver "
"to use in the comparison. "
"The range is not applied to the search string.";
NSLog(@"%@", [caseString uppercaseString]); //全大写
NSLog(@"%@", [caseString lowercaseString]); //全小写
NSLog(@"%@", [caseString capitalizedString]); //每个单词得首字母大写(以空格为介)
/* MSMutableString 可变字符串常用方法 */
//1.创建字符串对象
NSMutableString *mString = [NSMutableString stringWithFormat:@"今天很糟糕"];
//2.字符串拼接 改变原来得mString
[mString appendString:@"萌萌哒"];
NSLog(@"%@", mString);
//3.插入字符串
[mString insertString:@"真的" atIndex:2];
NSLog(@"%@", mString);
//4.删除字符串
NSRange r4 = {2, 2};
[mString deleteCharactersInRange:r4];
NSLog(@"%@", mString);
/*题(1)判断字符串@“aBcD_EfGk”中是否有EfGk,如果有将EfGk转换为WXYZ,并让字符串的大写字母转成小写,输出修改完成的字符串。
(2)给定一个图片文件名,判断字符串中是否以“png”结尾,如果是就替换成“jpg”,如果不是,就拼接”.jpg”。 */
//1.
NSString *st = [[NSString alloc] initWithFormat:@"aBcD_EfGk"];
NSRange ra = [st rangeOfString:@"EfGk"];
NSString *st1 = [[NSString alloc] init];
NSString *st2 = [[NSString alloc] init];
if (ra.length) {
st1 = [st stringByReplacingOccurrencesOfString:@"EfGk" withString:@"WXYZ"];//替换
NSLog(@"%@", st1);
for (NSInteger i = 0; i < ([st1 length]); i++) {
NSRange j = {i, 1}; //截取每一字符
NSString *s = [st1 substringWithRange:j];
if ([s compare:@"a"] < 0) {
NSString *x = [s lowercaseString];
st2 = [st1 stringByReplacingCharactersInRange:j withString:x];
NSLog(@"%@", st2);
}
}
}
//2.
NSString *music = [NSString stringWithFormat:@"1245.png"];
if ([music hasSuffix:@"png"]) {
NSString *music1 = [music stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
NSLog(@"%@", music1);
} else {
NSString *music2 = [music stringByAppendingString:@".jpg"];
NSLog(@"%@", music2);
}
}