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,一般 ...
随机推荐
- hihoCoder 1385 A Simple Job
#1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...
- adb remount 失败remount failed: Operation not permitted
1. 进入shell adb shell 2. shell下输入命令 shell@android:/ $ sushell@android:/ # mount -o rw,remount -t yaff ...
- HDU 4035Maze(树状+概率dp,绝对经典)
题意: 给你n个节点的树,从1节点开始走,到每个节点都有三种情况,被杀死回到1节点,找到隐藏的出口出去,沿着当前节点相邻的边走到下一个节点,给出每个节点三种情况发生的概率分别为ki,ei,1-ki-e ...
- HDU 4336-Card Collector(状压,概率dp)
题意: 有n种卡片,每包面里面,可能有一张卡片或没有,已知每种卡片在面里出现的概率,求获得n种卡片,需要吃面的包数的期望 分析: n很小,用状压,以前做状压时做过这道题,但概率怎么推的不清楚,现在看来 ...
- codeforces 678E Another Sith Tournament 概率dp
奉上官方题解 然后直接写的记忆化搜索 #include <cstdio> #include <iostream> #include <ctime> #include ...
- linux常用命令之--磁盘管理命令
linux的磁盘管理命令 1.查看磁盘空间 df:用于显示磁盘空间的使用情况 其命令格式如下: df [-option] 常用参数: -i:使用inodes显示结果 -k:使用KBytes显示结果 - ...
- switchomega配置
- 在DataTable 中增加一列
//在这里需要增加一个列. DataColumn column = dt.Columns.Add("行号", Type.GetType("S ...
- json字符串转换为JSONObject和JSONArray
一.下载json 具体到http://www.json.org/上找java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 二.具体转化过程 //JSONObject String ...
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇07:全屏炸弹》
7.全屏炸弹 全屏炸弹概述: 为了增设游戏的趣味性,我们制作一个游戏的基本框架以外.还会增设一些其他的额外的功能.比如5秒无敌状态.冰冻效果等.下面咱们以消灭屏幕中所有炸弹为例,看除了碰撞可以触发事件 ...