练习与示例:
1.字符串
NSString *str = [NSString stringWithFormat:@"我今年%d岁了",16];
NSLog(@"%@",str);
NSString *name = [NSString stringWithFormat:@"老王"];
NSLog(@"%@",name);
2.字符串的字面量形式(方便书写,语法糖🍬)
//@"字符串内容",得到的是一个字符串常量
NSString *name1 = @"老王";
NSLog(@"%@的长度为:%lu",name1,name1.length);
unichar c = [@"abc" characterAtIndex:0];
NSLog(@"%c",c);
NSString *name2 = @"老王";
if (name1 == name2) {
NSLog(@"指针地址相同");
} else {
NSLog(@"指针地址不同");
}
3.比较内容相同的方法
if ([name isEqualToString:name1]) {
NSLog(@"字符串内容相同");
} else {
NSLog(@"字符串内容不同");
}
4.字符串常用属性的使用
NSComparisonResult compareResult = [@"abc" compare:@"bcd"];
NSLog(@"%ld",compareResult);
//从索引值为1的位置开始截取子串(包含1的位置),一直截到最后
NSString *subString = [@"abcdefg" substringFromIndex:1];
NSLog(@"%@",subString);
//截取到从开始字符串到索引值处(不包含索引值位置)
NSString *subString1 = [@"abcdefg" substringToIndex:1];
NSLog(@"%@",subString1);
//截取某一段子串(range代表一个范围,location代表截取开始位置,length代表截取的长度)
NSString *subString2 = [@"abcdefgh" substringWithRange:NSMakeRange(1, 2)];
NSLog(@"%@",subString2);
//字符串拼接(在原有的字符串后面追加一个字符串)
NSString *appendString = [@"abc" stringByAppendingString:@"def"];
NSLog(@"appending:%@",appendString);
//在原有的字符串后面追加一个格式化字符串
NSString *appendString1 = [@"abc" stringByAppendingFormat:@"%d", 123];
NSLog(@"%@",appendString1);
//字符串替换
NSString *contentString = @"hi,dsb";
//获取dsb在contentString中所占的范围
NSRange range = [contentString rangeOfString:@"dab"];
NSLog(@"range:loc=%lu,len=%lu",range.location,range.length);
if (range.length != 0) {
//替换该范围的字符串为@"**"
NSString *replaceString = [contentString stringByReplacingCharactersInRange:range withString:@"**"];
NSLog(@"%@",replaceString);
}
NSString *replaceString1 = [contentString stringByReplacingOccurrencesOfString:@"dsb" withString:@"***"];
NSLog(@"%@",replaceString1);
//字符串转int
NSInteger number = [@"123.1" intValue];
NSLog(@"%ld",number + 1);
//其他数据类型转字符串
NSLog(@"%@", [NSString stringWithFormat:@"%ld", number]);
//转大写字母
NSString *uppercaseString = [@"abc" uppercaseString];
NSLog(@"转大写字母为:%@",uppercaseString);
//转小写字母
NSString *lowerString = [@"ADAFs" lowercaseString];
NSLog(@"转小写字母为:%@",lowerString);
//首字母大写
NSString *capitalizedString = [@"abc" capitalizedString];
NSLog(@"首字母大写为:%@",capitalizedString);
if ([@"abcdef" hasPrefix:@"ab"]) {
NSLog(@"ab是该字符串的前缀");
} else {
NSLog(@"ab不是该字符串的前缀");
}
5.可变字符串
NSMutableString *str1 = [[NSMutableString alloc]initWithString:@"abcdefg"];
NSMutableString *str2 = [[NSMutableString alloc]initWithString:@"abcdefg"];
//在原字符串上直接追加字符串
[str1 appendString:@"hij"];
//在元字符串上直接追加格式化字符串
[str2 appendFormat:@"%d",123];
NSLog(@"%@",str1);
NSLog(@"%@",str2);
//插入一个字符串
[str1 insertString:@"啦啦" atIndex:2];
NSLog(@"%@",str1);
//删除
[str1 deleteCharactersInRange:NSMakeRange(2, 1)];
NSLog(@"%@",str1);
//NSNumber(数字对象)
NSNumber *intNumber = [NSNumber numberWithInt:10];
NSInteger integer = intNumber.integerValue;
//字面量写法
NSNumber *numberObject = @10;
NSLog(@"%@", numberObject);
int x = 1;
NSNumber *numberObject1 = @(x);
NSLog(@"%@", numberObject1);
//值对象
NSValue *rangeValue = [NSValue valueWithRange:NSMakeRange(10, 10)];
NSLog(@"%@", rangeValue);
NSRange range1 = rangeValue.rangeValue;
作业:
1.将“文艺青年”改为“213青年”。
NSString *str1 = @"文艺青年";
str1 = [str1 stringByReplacingOccurrencesOfString:@"文艺" withString:@"213"];
NSLog(@"%@",str1);
2.将整数123, 转换为字符串“123”。
NSInteger number = [@"123" intValue];
NSLog(@"%@",[NSString stringWithFormat:@"%ld", number]);
3.将“i love you”中的单词首字母大写变成“I Love You”。
NSString *str2 = [@"i love you" capitalizedString];
NSLog(@"“i love you”中的单词首字母大写变成:%@",str2);
4.将“abc def ghi”中的空格去掉变成“abcdefghi”。
NSMutableString *str3 = [[NSMutableString alloc]initWithString:@"abc def ghi"];
[str3 deleteCharactersInRange:NSMakeRange(3, 1)];
[str3 deleteCharactersInRange:NSMakeRange(6, 1)];
NSLog(@"%@",str3);
5.判断一个字符串是否以http开头,以png结尾,如果是,输出是一个有效链接,如果不是,输出链接错误。
NSString *str4 = @"域名.png";
if ([str4 hasPrefix:@"http"] && [str4 hasSuffix:@"png"]) {
NSLog(@"有效连接:%@",str4);
} else {
NSLog(@"链接错误");
}
6.将整数123,存储为NSNumber类型的对象。
NSNumber *numberObject = @123;
NSLog(@"%ld",(long)numberObject);
7.将3.14159,存储为NSNumber类型的对象。
NSNumber *numberObject1 = @3.14159;
NSLog(@"%@",numberObject1);
8.将上述OC的NSNumber类型的对象转回为基本数据类型。
NSInteger number1 = numberObject.integerValue;
CGFloat number2 = numberObject1.floatValue;
NSLog(@"%ld",(long)number1);
NSLog(@"%f",number2);
9.判断一个字符串是否是一个QQ号(特点:5~12位数字,不以0开头),将结果打印在控制台,如@“123456”是一个QQ号。
NSString *string = @"123456";
if ([string hasPrefix:@"0"] && (string.length < 5 && string.length > 12)) {
NSLog(@"不是一个QQ号");
} else {
NSLog(@"%@是一个QQ号",string);
}
10.判断一个字符串是否是手机号(特点:11位数字,以13或14或15或17或18开头)
NSString *phoneNumber = @"13322348712";
if (([phoneNumber hasPrefix:@"13"] || [phoneNumber hasPrefix:@"14"] || [phoneNumber hasPrefix:@"15"] || [phoneNumber hasPrefix:@"17"] || [phoneNumber hasPrefix:@"18"]) && (phoneNumber.length > 0 && phoneNumber.length < 12)) {
NSLog(@"%@是一个手机号码", phoneNumber);
} else {
NSLog(@"不是一个手机号");
}
11.NSString *htmlString = @“<html><head><title>震惊,深夜温强竟然做出这种事</title></head><body><p>从前有座山,山里有座庙,庙里有个老温强和一个小温强,老温强对小温强说:\”嘿嘿嘿"</p></body></html>";
要求:
1.将上面这个字符串中的<title></title>中间的内容作为一个新的字符串提取出来
2.将上面这个字符串中的<p></p>中间的内容作为一个新的字符串提取出来
注意:<title></title>和<p></p>假设位置不固定,不要人为的查位置截取
//求出
NSRange range=[htmlString rangeOfString:@"<title>"];
//NSString * strt = NSStringFromRange(range);
NSRange range1=[htmlString rangeOfString:@"</title>"];
//NSString * strt1 =NSStringFromRange(range1);
//NSLog(@"r1:%@,r2:%@",strt,strt1);
NSUInteger loc = range.location+range.length;
NSUInteger lenth=range1.location-loc;
NSRange r=NSMakeRange(loc, lenth);
NSString *final=[htmlString substringWithRange:r];
NSLog(@"这个字符串中的<title></title>中间的内容是:%@",final);
NSRange secRange = [htmlString rangeOfString:@"<p>"];
NSRange secRange1 = [htmlString rangeOfString:@"</p>"];
NSUInteger location = secRange.location+secRange.length;
NSUInteger lenth1 = secRange1.location-location;
NSRange r1=NSMakeRange(location, lenth1);
NSString *result = [htmlString substringWithRange:r1];
NSLog(@"这个字符串中的<p></p>中间的内容是:%@",result);
NSString *s1=[htmlString stringByReplacingOccurrencesOfString:@"<html><head><title>" withString:@""];
NSString *s2=[s1 stringByReplacingOccurrencesOfString:@"</title></head><body><p>从前有座山,山里有座庙,庙里有个老温强和一个小温强,老温强对小温强说:”嘿嘿嘿\"</p></body></html>" withString:@""];
NSLog(@"%@",s2);