iOS NSString常用用法大全
版权声明:本文为博主Atany原创文章,未经博主允许不得转载。博客地址:http://blog.csdn.net/yang8456211
一、NSRange
在对NSString介绍之前,我们先要了解一个结构体NSRange。
- typedef struct _NSRange
- {
- unsignedint location;
- unsignedint length;
- }NSRange;
NSRange表示相关事物的一个范围,常与字符串操作一起使用。
Location时字段的起始位置,length为字段的长度。
例如:
- NSRange range = [@"I am atany" rangeOfString:@"am"];//查找“am”字符串在“I am atany”中的位置。
- NSLog(@"%d,%d",range.location,range.length);//2,2
声明一个NSRange有几种方法。
第一种:
- NSRange range;
- range.location = 0;
- range.length = 1;
第二种:
- NSRange range = {0,1};
第三种:
- NSRange range = NSMakeRange(0, 1);
二、NSString
下面介绍一些NSString常用的方法。
1、字符串的声明
1)直接赋值常量
- NSString *string1 = @"hello";
2)先分配内存初始化再赋值
- NSString *string2 = [[NSStringalloc]init];
- string2= @"hello";
3)使用工厂方法制造对象
- NSString *string3 = [NSStringstringWithFormat:@"hello"];
4)使用initWithString与initWithFormat方法初始化
- NSString *string4 = [[NSStringalloc]initWithString:@"hello"];
- NSString *string4 = [[NSStringalloc]initWithFormat:@"hello"];
5)使用C字符串生成NSString
- NSString *string5 = [[NSStringalloc]initWithCString:"hello"encoding:NSUTF8StringEncoding];
6)读取本地文件里面的字符串,hello.text为相对路径,存放在项目中
- NSString *path = [[NSBundlemainBundle] pathForResource:@"hello.txt"ofType:nil];
- NSString *string6 = [[NSStringalloc] initWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];
7)使用已有的字符串生成
- NSString *string7 = [NSStringstringWithString:string6];
2、获取字符串的长度
- NSString *str = [NSString stringWithFormat:@"first"];
- NSLog(@"str length is %d",str.length); //字符串长度
3、转化字符串的大小写
- NSString *string = @"hEllO";
- NSLog(@"string is %@",[string uppercaseString]);//转化为大写
- NSLog(@"string is %@",[string lowercaseString]);//转化为小写
- NSLog(@"string is %@",[string capitalizedString]);//转化为首字母大写
输出:
string is HELLO
string is hello
stringis Hello
4、判断字符串前缀与后缀
- NSString *hello = @"hello.png";//0代表匹配,1代表不匹配
- NSLog(@"hello has prefix %d",[hello hasPrefix:@"hello"]);//以hello开头
- NSLog(@"hello has suffix %d",[hello hasSuffix:@".png"]);//以.png结尾
可以方便的实现判断文件类型等操作。
5、判断字符串是否相等
如果相等,会返回YES,否则NO
- NSString *string1 = @"hello";
- NSString *string2 = [NSString stringWithFormat:@"hello"];
- NSString *string3 = @"hi";
- if([string1 isEqualToString:string2]){
- NSLog(@"string1 is Same to string2");
- }
- else{
- NSLog(@"string1 is different to string2");
- }
- if([string1 isEqualToString:string3]){
- NSLog(@"string2 is Same to string3");
- }
- else{
- NSLog(@"string2 is different to string3");
- }
输出结果:
string1 is Same to string2
string2 is different to string3
6、包含其他字符串
使用rangeOfString返回只一个NSRange参数,通过range的location与length可以方便的找到包含字符串
- NSString *string1 = @"hello";
- NSString *string2 = @"I am hello123";
- NSRange range = [string2 rangeOfString:string1];
- NSLog(@"range.location is %d,range.length is %d",range.location,range.length);
输出:
range.location is 5,range.length is 5
如果不包含字符串,那么在range.location会等于NSNotFound
7、字符串的比较
字符串的比较使用 compare的方法
返回值:NSComparisonResult
,[@"hello" compare:@"hello"]);
- NSLog(@"flag is %d",[@"hello" compare:@"aello"]);
- NSLog(@"flag is %d",[@"hello" compare:@"hello1"]);
- NSLog(@"flag is %d",[@"hello" compare:@"Hello"]);//h在H之后
结果为:
flag is 0
flag is 1
flag is -1
flag is 1
8、不区分大小写的比较
使用compare:options,options参数是一个掩位码,可以用“|”符号使用多个。
NSCaseInsensitiveSearch:不区分大小写
NSLiteralSearch:区分大小写
NSNumericSearch:比较字符串的个数,而不是子字符值
- NSLog(@"%d",[@"hello" compare:@"Hello" options:NSCaseInsensitiveSearch]);//返回值为0
- NSLog(@"%d",[@"hello" compare:@"Hello" options: NSLiteralSearch]);//返回值为1
- NSLog(@"%d",[@"Name7.txt" compare:@"Name25.txt" options:NSNumericSearch]); //返回值为-1
- NSLog(@"%d",[@"Name7.txt" compare:@"Name25.txt"]);//返回值为1
9、截取字符串
- NSString *string = @"hello world";
- NSString *string2 = [string substringToIndex:2];//2为长度(从0开始截取)
- NSLog(@"string2 is %@",string2);
- NSString *string3 = [string substringFromIndex:2];//从2开始(截取到最后)
- NSLog(@"string3 is %@",string3);
- NSRange range = { 2,2 };//从2开始截取,截取2位
- NSString *string4 = [string substringWithRange:range];
- NSLog(@"string4 is %@",string4);
输出:
string2 is he
string3 is llo world
string4 is ll
10、去除字符串首尾的空格
- NSString *text = [@" hello " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- NSLog(text);//hello
11、NSMutableString
NSString创建之后不可变的,而NSMutableString为可变的,类似与java中的string与stringBuffer。
NSMutableString为NSString子类,继承所有方法。
1)创建字符串
50这个值只是一个参考,不够的话系统会自动补充。
- NSMutableString *string = [NSMutableStringstringWithCapacity:50];
2)在字符串尾添加新的字符串
- [string appendString:@"hi"];
- NSLog(@"string is %@",string);//string is hi
3)使用Format形式添加新的字符串
- [string appendFormat:@" I am %@",@"atany"];
- NSLog(@"string is %@",string);//string is hi I am atany
4)删除字符串
- NSRange range = [string rangeOfString:@"am"];//找回”am”字符串位置(5,2)
- [string deleteCharactersInRange:range];//先用rangeOfString来得到range
- NSLog(@"string is %@",string);// string is hi I atany
5)在字符串中插入字符串
- [string insertString:@"York"atIndex:1];//插入到h之后
- NSLog(@"string is %@",string);// string is hYorki I atany
- [string setString:@"replace"];//改变字符串
- NSLog(@"string is %@",string);// string is replace
- [string replaceCharactersInRange:range withString:@"is"];
- NSLog(@"string is %@",string);// string is replais
注:range只是记录的一个字符串位置,而不是字符串,此时range为上次rangeOfString:@"am"的位置(5,2)。
**************************************************************
atany原创,转载请注明博主与博文链接,3Q
http://blog.csdn.net/yang8456211/article/details/11744505
—— by atany
**************************************************************
iOS NSString常用用法大全的更多相关文章
- 转发:iOS之textfield用法大全
转发至:http://m.blog.csdn.net/article/details?id=8121915 //初始化textfield并设置位置及大小 UITextField *text = [[U ...
- 转帖: 一份超全超详细的 ADB 用法大全
增加一句 连接 网易mumu模拟器的方法 adb connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...
- 开源 iOS 项目分类索引大全 - 待整理
开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...
- IOS开发常用设计模式
IOS开发常用设计模式 说起设计模式,感觉自己把握不了笔头,所以单拿出iOS开发中的几种常用设计模式谈一下. 单例模式(Singleton) 概念:整个应用或系统只能有该类的一个实例 在iOS开发我们 ...
- OC常用数据类型大全解
UI基础 OC常用数据类型 Block Block封装了一段代码,可以在任何时候执行 Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很类似,但是有区别 ...
- MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part1.html 上一节 ...
- ios中常用数据类型相互转换
ios中常用数据类型相互转换 //1. NSMutableArray和NSArray互转 // NSArray转为NSMutableArray NSMutableArray *arrM = [arr ...
- javascript常用代码大全
http://caibaojian.com/288.html 原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...
- iOS中常用的四种数据持久化方法简介
iOS中常用的四种数据持久化方法简介 iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 ...
随机推荐
- DBCP连接池原理分析及配置用法
DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...
- gradle 学习
gradle是个构建工具,目的是为了更方便的管理项目. 学习gradle看下面的资料: 中文资料,总共六篇,看完之后基础差不多了: 简介 第一个Java项目 依赖管理 创建二进制发布版本 创建多项目构 ...
- bjfu1099 度度熊大战僵尸
这也是2011年百度之星的一道题. 这题我就是乱搞搞过的,打代码之前自己心里也没底,不知道能不能过的. 我的做法很简单,就是按时间顺序依次构造能杀死的僵尸血量,找到第k小的.构造的方法也很暴力:对t时 ...
- UITextView 相关知识点
1.得到UITextView的高度 - (CGRect)contentSizeRectForTextView:(UITextView *)textView { [textView.layoutMana ...
- 【剑指offer 面试题14】调整数组顺序使奇数位于偶数前面
思路: 头尾指针,向中间遍历,依据条件交换元素. #include <iostream> using namespace std; void reOrder(int *pData, uns ...
- Richedit使用大全
原文地址:http://blog.csdn.net/pcseye/article/details/3903333 一.常见问题 a.可以编译,不能执行的 AfxInitRichEdit(); b.升级 ...
- 关于webpack最好的文档
这几天研究webpack打包工具,在网上搜了无数的资料,鱼龙混杂.看了几十份资料,依然没有一个可以完整的描述的. 折腾了那么久,还是放弃治疗了.回到官网,一字一句的阅读,一个小时就彻底明白了. 学习新 ...
- RTNETLINK answers: File exists错误
解决ssh连接虚拟机出错,RTNETLINK answers: File exists 解决步骤如下: 使用ssh连接虚拟机的时候,发现目标主机无法连接,登录虚拟机,查看ssh监听是否开启: 发现监听 ...
- 有关OOM KILLER的一些理解
Linux下有一种OOM KILLER 的机制,它会在系统内存耗尽的情况下,启用自己算法有选择性的kill 掉一些进程. 一.为什么会有OOM killer 当我们使用应用时,需要申请内存,即进行ma ...
- JSF 2 textbox example
In JSF, you can use the <h:inputText /> tag to render a HTML input of type="text", t ...