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,一般 ...
随机推荐
- sql server 2012序列号
MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28 MICROSOFT SQL SERVER 2012 商业智能版 ...
- [转] AE之分级颜色专题图渲染
原文 AE之分级颜色专题图渲染 参考代码1 private void 分级渲染ToolStripMenuItem_Click(object sender, EventArgs e) { //值分级 I ...
- DevExpress z
1.TextEditor(barEditItem)取文本 string editValue = barEditItem1.EditValue.ToString(); //错误,返回null st ...
- Hadoop Configuration
Configuration的主要是加载配置文件,并储存在properties中. 细节内容不重复了,主要参考Hadoop技术内幕,Hadoop源代码,以及: http://blog.csdn.net/ ...
- select的option异常的总结
来源:http://www.ido321.com/1189.html 昨天,在项目中碰到了option显示异常的原因,截图如下: Firefox中用css控制之后效果 chrome和IE中css不奏效 ...
- centos编译helloworld的几个小问题
1.GCC使用在使用GCC编译程序时,编译过程可以被细分为四个阶段:预处理(Pre-Processing)编译(Compiling)汇编(Assembling)链接(Linking).例如: ...
- Linux 中 x86 的内联汇编
工程中需要用到内联汇编,找到一篇不错的文章,趁机学习下. 原文地址:http://www.ibm.com/developerworks/cn/linux/sdk/assemble/inline/ 如果 ...
- [Hive - LanguageManual] Hive Default Authorization - Legacy Mode
Disclaimer Prerequisites Users, Groups, and Roles Names of Users and Roles Creating/Dropping/Using R ...
- NodeJS学习:爬虫小探
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 今天来学习alsotang的爬虫教程,跟着把CNode简单地爬一遍. 建立项目craelr-demo 我们首先建立一个Expr ...
- work8
使用裸指针: #include <iostream>#include <memory>#include <stdio.h>#include <cstring& ...