NSString NSMutableString
// NSString
//代开API文档
//Xcode -> help - Documentation and API Reference
//快速打开API
//alt(option) + 鼠标左键
//快速进入头文件
//command + 鼠标左键
// NSString是oc中的不可变字符串类,被创建后,不能修改
//常用方法
//1.创建字符串
NSString *str1 = @"iPhone";
NSLog(@"%@", str1);
NSString *str2 = [[NSString alloc] init];
NSLog(@"%@", str2);
NSString *str3 = [[NSString alloc] initWithString:@"iPhone"];
NSLog(@"%@", str3);
// NSString *str4 = [[NSString alloc] initWithString:nil];
// NSLog(@"%@", str4); error
NSString *str5 = [[NSString alloc] initWithFormat:@"%@5s", str1];
NSLog(@"%@", str5);
// NSString *str6 = [NSString stringWithString:@"iPhone"];
// NSLog(@"%@", str6);
NSString *str7 = [NSString stringWithFormat:@"123"];
NSLog(@"%@", str7);
//2.获取字符串长度
NSUInteger length = [str7 length];
NSLog(@"%lu", length);
//3.判断字符串是否以指定字符串开始或结束
NSString *str8 = @"123ABC";
BOOL result = [str8 hasPrefix:@"223"];
NSLog(@"%d", result);
BOOL result1 = [str8 hasSuffix:@"C"];
NSLog(@"%d", result1);
//4.搜索字符串的范围
NSRange range = [str8 rangeOfString:@"3A"];
NSLog(@"%lu, %lu", range.length, range.location);
//5.字符串截取
NSRange rs = {3, 3};
NSString *str9 = [str8 substringWithRange:rs];
NSLog(@"%@", str9);
//6.拼接字符串
NSString *str10 = [str8 stringByAppendingString:@"123"];
NSLog(@"%@", str8);
NSLog(@"%@", str10);
//7.替换字符串
NSString *str11 = [str8 stringByReplacingOccurrencesOfString:@"3A" withString:@"A"];
NSLog(@"%@", str11);
//8.字符串比较"123" "123" "321"
NSInteger flag = [@"123" compare:@"123"];
NSLog(@"%ld", flag);
//9.字符串和数值类型的转换
NSString *str12 = @"123";
int a = [str12 intValue];
NSLog(@"%d", a);
double b = [str12 doubleValue];
NSLog(@"%f", b);
float c = [str12 floatValue];
NSLog(@"%f", c);
BOOL e = [str12 boolValue];
NSLog(@"%d", e);
//10.大小写转换操作
NSString *aaa = @"aBcE";
NSLog(@"%@", [aaa capitalizedString]);
NSLog(@"%@", [aaa uppercaseString]);
NSLog(@"%@", [aaa lowercaseString]);
NSLog(@"%@", aaa);
//NSMutableString
//capctiy 是一个预估的值,可以改变
//由于内存比较紧张,一般把capctiy设置成0,让字符串自己去判断大小,扩充容器大小
//创建一个新的字符串
NSMutableString *mString = [[NSMutableString alloc] initWithCapacity:0];
//拼接字符串
[mString appendString:@"taiyang"];
NSLog(@"%@", mString);
//插入字符
[mString insertString:@"aaa" atIndex:1];
NSLog(@"%@", mString);
//删除字符串
// NSRange r = {2, 3};
// [mString deleteCharactersInRange:r];
// || 等价
[mString deleteCharactersInRange
练习 :
// 1.截取字符串“20|http://www.baidu.com”中 “|” 前面和后面的字符串,并输出。
// NSString *str1 = @"20|http://www.baidu.com";
// 1
// NSString *str2 = [str1 substringWithRange:NSMakeRange(0, 2)];
// NSString *str3 = [str1 substringWithRange:NSMakeRange(3, 20)];
// NSLog(@"str2 = %@ str3 = %@", str2, str3);
//2
// NSString *str2 = [str1 substringFromIndex:3];
// NSLog(@"str2 = '%@'", str2);
// NSString *str3 = [str1 substringToIndex:2];
// NSLog(@"str3 = '%@'", str3);
// 2.将“文艺青年”改成“213青年”。
// NSString *str1 = @"文艺青年";
// NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"文艺" withString:@"213"];
// NSLog(@"%@", str2);
// 3.给定一个图片文件名,判断字符串中是否有后缀,如果有(如:以“.png”结尾),就替换成“jpg”;如果没有,就拼接”.jpg”
// NSString *str1 = @"taiyang";
// NSUInteger length = [str1 length];
// NSString *str2 = [str1 substringFromIndex:length - 4];
// if ([@".jpg" compare:str2] == 0) {
// NSLog(@"该字符串是以.jpg开头");
// }else if ([@".png" compare:str2] == 0){
// NSString *str3 = [str1 stringByReplacingOccurrencesOfString:@".png" withString:@".jpg"];
// NSLog(@"str3 = '%@'", str3);
// }else{
// NSString *str4 = [str1 stringByAppendingString:@".jpg"];
// NSLog(@"str4 = '%@'", str4);
// }
NSString NSMutableString的更多相关文章
- NSString&NSMutableString常用操作梳理(转)
作者:弦苦 授权本站转载. 上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Objective-C中每天都要用到的字符串处理类——NSString. Objec ...
- 关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary
NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎 ...
- NSString&NSMutableString常用操作梳理
http://www.cocoachina.com/ios/20150724/12722.html 上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Object ...
- NSString NSMutableString copy mutableCopy retain weak strong整合
copy retain assign的差别在于对象属性的set方法 NSString 与 NSMutableString NSString是不可变字符串对象,这句话的意思,结合代码: #import ...
- [转] NSString / NSMutableString 字符串处理,常用代码
原文 : http://justcoding.iteye.com/blog/1405951 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString , ...
- 【转】 NSString / NSMutableString 字符串处理,常用代码 (实例)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- NSString / NSMutableString 字符串处理,常用代码 (实例)
http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...
- Objective-C NSString/NSMutableString
创建于完成: 2018/02/05 总览: http://www.cnblogs.com/lancgg/p/8404975.html 字符串类 简介 字符码: Unicode NSString ...
- 转:用法总结:NSNumber、NSString、NSDate、NSCalendarDate、NSData(待续)
NSNumber + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithDouble:(double)value; - (in ...
随机推荐
- oc NSLog输出格式大全
本文的内容是总结了一下iOS开发中NSLog输出格式大全,虽然比较基础,但有总结毕竟会各位正在学习iOS开发的朋友们一些小小的帮助. %@ 对象 %d, %i ...
- do{}while() ;异常语句
//while (true) //只要括号里面是true(正确的如:(1==1)),就会无限循环 //{ //} //do{}while() //不管while满足与否,首先先做一遍 //然后去看wh ...
- mysql中binlog_format的三种模式
mysql复制主要有三种方式:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复 ...
- 配置Tomcat 7 Gzip
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" ...
- Spring依赖注入:基于xml配置
基础接口 BeanFactory.ApplicationContext. BeanFactory用于创建并管理.获取各种类的对象. ApplicationContext从BeanFactory派生而来 ...
- hdoj1087 (DP--LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...
- hdoj1251-统计难题 【字典树】
http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- Python repr() 函数
Python repr() 函数 Python 内置函数 描述 repr() 函数将对象转化为供解释器读取的形式. 语法 以下是 repr() 方法的语法: repr(object) 参数 obje ...
- 10.Regular Expression Matching (String; Back-Track,DP)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- ECMAScript5新特性之isSealed、seal
封闭对象后: 1 不能增加属性. 2 不能删除属性. 3 可以修改属性.(赋值) 4 不能修改属性描述符.(抛异常) var fruit = { name : '苹果', desc : '红富士' } ...