#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1⃣️.通过C的字符串
//C --> OC
char *cString = "cString";
NSString *ocStr1 = [[NSString alloc]initWithUTF8String:cString];
NSString *ocStr2 = [NSString stringWithUTF8String:cString];
NSString *ocStr3 = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
NSLog(@"%@",ocStr1);
NSLog(@"%@",ocStr2);
NSLog(@"%@",ocStr3);
//OC --> C
const char *cStr = [ocStr1 UTF8String];
NSLog(@"%s",cStr);
//2⃣️.初始化
//1.实例方法
//快速创建:更改字符串 --> 更改了指针的指向地址
NSString *string = @"string";
string = @"我得字幕组啊";
NSString *str2 = [[NSString alloc]initWithFormat:@"%@唱战歌!",string];
NSLog(@"%@",str2);
//2.类方法
NSString *str4 = [NSString stringWithFormat:@"%d %@ %f",99,string,66.66];
NSLog(@"%@",str4);
//3⃣️属性
NSUInteger length = string.length;
NSLog(@"%ld",length);
//4⃣️方法
//判断字符串的内容是否相同
BOOL isEqual = [string isEqualToString:str2];
NSLog(@"%d",isEqual);
//判断字符串的地址是否相同
NSLog(@"%@",(str2 == str4)?@"地址相同":@"地址不同");
//比较字符串的大小 --> 升序 相同 降序
NSComparisonResult result = [@"bbb"caseInsensitiveCompare:@"aaa"];
switch (result) {
case NSOrderedAscending:
NSLog(@"升序");
break;
case NSOrderedSame:
NSLog(@"平序");
break;
case NSOrderedDescending:
NSLog(@"降序");
break;
default:
break;
}
//√ 转换:大小字母转换
NSString *str5 = @"qwerQAQ";
//1.全部变大写字母
NSLog(@"%@",[str5 uppercaseString]);
//2.全部变小写字母
NSLog(@"%@",[str5 lowercaseString]);
//3.首字母大写 --> 每个单词的首字母大写,其他小写
NSLog(@"%@",[str5 capitalizedString]);
//str5还是原来的
NSLog(@"%@",str5);
//√ 转化:基本数据类型
NSString *str6 = @"10086";
int intS = [str6 intValue];
NSInteger intsS = [str6 integerValue];
float floS = [str6 floatValue];
double douS = [str6 doubleValue];
BOOL boolS = [str6 boolValue];
NSLog(@"%d",intS);
NSLog(@"%ld",intsS);
NSLog(@"%f",floS);
NSLog(@"%lf",douS);
NSLog(@"%d",boolS);
//字符串的截取
//1.分割
NSString *urlString = @"www.a4yy.com";
//将参数作为分隔符,将字符串分割成若干部分
NSArray *array = [urlString componentsSeparatedByString:@"."];
NSLog(@"%@",array);
//2.获取字符串
char character = [urlString characterAtIndex:4];
NSLog(@"%c",character);
//3.截取substring
//截取到下标(不包括下标字符)
NSLog(@"%@",[urlString substringToIndex:5]);
//自下标截取(包括下标字符)
NSLog(@"%@",[urlString substringFromIndex:5]);
//OC创建结构体
NSRange range = NSMakeRange(4, 5);
NSLog(@"%@",[urlString substringWithRange:range]);
//4.拼接 appending
NSString *appString = @"南山南";
//拼接字符串
NSLog(@"%@",[appString stringByAppendingString:@"北秋悲"]);
//拼接格式化字符串
NSLog(@"%@",[appString stringByAppendingFormat:@"最丑不过%@",@"卢本伟"]);
//拼接扩展名 .pdf
NSLog(@"%@",[appString stringByAppendingPathExtension:@"dmg"]);
//拼接路径 /path
NSLog(@"%@",[appString stringByAppendingPathComponent:@"dmg"]);
//5.查找
NSString *findString = @"/Users/CORYIL/Library/Developer/CoreSimulator/Devices/88A633A0-6294-4853-80DE-8060C1580820/data/Containers/Data/Application/84CE535F-49D1-498E-89DB-2F07C50D6A24/Documents/";
NSRange findResult = [findString rangeOfString:@"Documents"];
//将结构体转化为字符串 NSStringFromXXX(结构体)
NSLog(@"%@",NSStringFromRange(findResult));
//替换 replacing
//根据range替换
NSString *newString = [findString stringByReplacingCharactersInRange:findResult withString:@"黑黑"];
NSLog(@"%@",newString);
//直接替换
NSLog(@"%@",[findString stringByReplacingOccurrencesOfString:@"Users" withString:@"用户们"]);
}
return 0;
}
NSL…
2016-02-18 11:25:07.816 NSString[1162:64272] cString
2016-02-18 11:25:07.818 NSString[1162:64272] cString
2016-02-18 11:25:07.818 NSString[1162:64272] cString
2016-02-18 11:25:07.819 NSString[1162:64272] cString
2016-02-18 11:25:07.819 NSString[1162:64272] 我得字幕组啊唱战歌!
2016-02-18 11:25:07.820 NSString[1162:64272] 99 我得字幕组啊 66.660000
2016-02-18 11:25:07.820 NSString[1162:64272] 6
2016-02-18 11:25:07.820 NSString[1162:64272] 0
2016-02-18 11:25:07.820 NSString[1162:64272] 地址不同
2016-02-18 11:25:07.821 NSString[1162:64272] 降序
2016-02-18 11:25:07.821 NSString[1162:64272] QWERQAQ
2016-02-18 11:25:07.821 NSString[1162:64272] qwerqaq
2016-02-18 11:25:07.822 NSString[1162:64272] Qwerqaq
2016-02-18 11:25:07.822 NSString[1162:64272] qwerQAQ
2016-02-18 11:25:07.822 NSString[1162:64272] 10086
2016-02-18 11:25:07.822 NSString[1162:64272] 10086
2016-02-18 11:25:07.822 NSString[1162:64272] 10086.000000
2016-02-18 11:25:07.823 NSString[1162:64272] 10086.000000
2016-02-18 11:25:07.823 NSString[1162:64272] 1
2016-02-18 11:25:07.823 NSString[1162:64272] (
www,
a4yy,
com
)
2016-02-18 11:25:07.823 NSString[1162:64272] a
2016-02-18 11:25:07.824 NSString[1162:64272] www.a
2016-02-18 11:25:07.824 NSString[1162:64272] 4yy.com
2016-02-18 11:25:07.824 NSString[1162:64272] a4yy.
2016-02-18 11:25:07.824 NSString[1162:64272] 南山南北秋悲
2016-02-18 11:25:07.824 NSString[1162:64272] 南山南最丑不过卢本伟
2016-02-18 11:25:07.825 NSString[1162:64272] 南山南.dmg
2016-02-18 11:25:07.825 NSString[1162:64272] 南山南/dmg
2016-02-18 11:25:07.825 NSString[1162:64272] {161, 9}
2016-02-18 11:25:07.825 NSString[1162:64272] /Users/CORYIL/Library/Developer/CoreSimulator/Devices/88A633A0-6294-4853-80DE-8060C1580820/data/Containers/Data/Application/84CE535F-49D1-498E-89DB-2F07C50D6A24/黑黑/
2016-02-18 11:25:07.826 NSString[1162:64272] /用户们/CORYIL/Library/Developer/CoreSimulator/Devices/88A633A0-6294-4853-80DE-8060C1580820/data/Containers/Data/Application/84CE535F-49D1-498E-89DB-2F07C50D6A24/Documents/
Program ended with exit code: 0
Day.02.18 NSString
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- NSString过滤特殊字符 去除所有空格 去除收尾的空格 复制字符串到剪贴板 处理标签字符串中的空格,换行,/t...
- 还有两天就胜利在望了,宝宝们都在疯狂补作业和修订重做的节奏。当日收到作业5篇,为你们的精进精神点赞! 很有分量的鱼...