0bjective-c 之 NSString 使用详解
查找资料的时候发现不错的文章,自己翻译之后分享给大家!
一个基本的该类型字符串例子:
@"This is a constant character string object";
在后台显示它:
NSLog (@"%@", @"This is a constant character string object");
得到其长度:
int len = [@"Hello" length];
NSLog (@"Length of string = %i", len);
结果是:
Length of string = 5
注意,空格也算一个。只是这个例子里没有而已。
创建可变或者不可变的字符串:
OBJ-C的字符串分为 mutable and immutable两种(可变和不可变)我们通常声明的 NSString是不可变的,一旦创建你休想用任何方式改变它!例如:
NSString *string1 = @"This string is immutable";
可变字符串是可以有很多方法可以用的,但是我们不能像刚才那样直接赋值,而是必须采用复制的方式来弄,例如:
NSMutableString *string2 = [NSMutableString stringWithString:@"This string is mutable"];
如果你想改变不可见字符串,那么只有一个办法,那就是再声明一个可变的字符串,然后把内容复制进入,例如:
NSString *string1 = @"This is a string";
NSMutableString *string2;
string2 = [NSMutableString stringWithString: string1];
复制一个字符串:
如果是INT类型我们大可以这样做:
int a = 10;
int b;
a = b;
但是对于字符串来说我们用下面这种做法是很愚蠢的:
NSMutableString *string1;
NSMutableString *string2;
string1 = [NSMutableString stringWithString: @"This is a string"];
string2 = string1;
定义的字符串变量都是指针,上面这种赋值方式只是让两个字符串指向同一块内存而已,所以我们测试一下就知道了:
NSMutableString *string1;
NSMutableString *string2;
string1 = [NSMutableString stringWithString: @"This is a string"];
string2 = string1;
[string2 appendString: @" and it is mine!"];
NSLog (@"string1 = %@", string1);
NSLog (@"string2 = %@", string2);
结果果然是这样的:
2009-11-03 14:35:37.731 t[32239:10b] string1 = This is a string and it is mine!
2009-11-03 14:35:37.732 t[32239:10b] string2 = This is a string and it is mine!
所以我们必须使用下面的方法来复制:
NSMutableString *string1;
NSMutableString *string2;
string1 = [NSMutableString stringWithString: @"This is a string"]; // Initialize string1
string2 = [NSMutableString stringWithString: string1]; // Copy string1 object to string2
[string2 appendString: @" and it is mine!"]; // Modify string2
NSLog (@"string1 = %@", string1);
NSLog (@"string2 = %@", string2);
让我们来看一下结果吧,没有问题了:
2009-11-03 14:42:10.426 t[32263:10b] string1 = This is a string
2009-11-03 14:42:10.427 t[32263:10b] string2 = This is a string and it is mine!
对字符串进行检索:
我们可以使用 rangeOfString method 来对字符串进行检索 这个方法返回一个 NSRange 类型的变量,下面我们来看具体使用例子:
NSString *string1 = @"The quick brown fox jumped";
NSRange match;
match = [string1 rangeOfString: @"brown fox"];
NSLog (@"match found at index %i", match.location);
NSLog (@"match length = %i", match.length);
location 的意思就是所寻找的字符串的起始位置,注意是从0开始数而不是1,length 就是找到的这个字符串有多长,这个是从1开始数的,所以我们可以知道上面的例子中location 的值是10,而length的值是9。
如果没有找到,系统会给location 设置成NSNotFound下面是个例子:
NSString *string1 = @"The quick brown fox jumped";
NSRange match;
match = [string1 rangeOfString: @"brown dog"];
if (match.location == NSNotFound)
NSLog (@"Match not found");
else
NSLog (@"match found at index %i", match.location);
替换字符串的某一部分:
我们可以用replaceCharactersInRange方法来替换字符串的某一部分,它需要2个参数,第一个参数是一个NSRange类型的,它必须指出我们从哪个坐标开始替换,以及替换几个字母,我们可以利用NSMakeRange来方便的搞定这个参数,第二个参数就是我们具体要替换的字符串了,下面是例子:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
[string1 replaceCharactersInRange: NSMakeRange(16, 3) withString: @"squirrel"];
NSLog (@"string1 = %@", string1);
替换成功了,我们替换的新字符串不一定和以前的一样长,替换之后它会自动调整长度的。
检索与替换配合使用:
我们可以把以上两个方法放在一起用,这样我们就不用去数数了,很方便的,看例子吧:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
[string1 replaceCharactersInRange: [string1 rangeOfString: @"brown fox"] withString: @"black dog"];
我们得到的结果是: "The quick black dog jumped".
删除字符串的一部分:
这个方法的使用方法和上面差不多,我们依然可以使用 rangeOfString方法来帮助它,看例子:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
[string1 deleteCharactersInRange: [string1 rangeOfString: @"jumped"]];
提取出字符串的一部分:
使用substringWithRange方法就可以做到这些了,它只需要你提供你希望提取的位置和长度即可:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
NSString *string2;
string2 = [string1 substringWithRange: NSMakeRange (4, 5)];
NSLog (@"string2 = %@", string2);
我们的结果自然是:quick
我们还可以使用 subStringFromIndex方法来得到制定位置之后所有的字符串,我们只需要提供一个位置参数:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
NSString *string2;
string2 = [string1 substringFromIndex: 4];
结果是什么就不用说了吧,很简单。
在字符串中插入一段文本:
有了上面的基础,直接看例子就可以明白了:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
[string1 insertString: @"agile, " atIndex: 4];
在字符串最后添加一段文本:
直接看例子:
NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];
[string1 appendString: @" over the lazy dog"];
NSLog (@"string1 = %@", string1);
连接两个字符串:
NSString* string; // 结果字符串
NSString* string1, string2; //已存在的字符串
1. string = [NSString initWithFormat:@"%@,%@", string1, string2 ];
2. string = [string1 stringByAppendingString:string2];</p>
3 . string = [string stringByAppendingFormat:@"%@,%@",string1, string2];
比较两个字符串:
我们要比较两个字符串是否相等,看看下面的例子:
NSString *string1 = @"My String";
NSString *string2 = @"My String";
if (string1 == string2)
NSLog (@"Strings match");
else
NSLog (@"Strings do not match");
请一定不要这样做,这样比较的是两个字符串指向的是否是同一块内存,而并不是他们的字符是否相等,所以上面的例子答案是不相等的,那么我们应该按照下面这样做才是正确的:
NSString *string1 = @"My String";
NSString *string2 = @"My String 2";
if ([string1 isEqualToString: string2])
NSLog (@"Strings match");
else
NSLog (@"Strings do not match");
查看字符串的前缀和后缀:
我们用 hasPrefix 和 hasSuffix 方法来查看字符串的前后缀,它们返回一个BOOLEAN类型的变量告诉我们字符串的前后缀到底是不是我们输入的字母,下面是例子:
NSString *string1 = @"The quick brown fox jumped";
BOOL result;
result = [string1 hasPrefix: @"The"];
if (result)
NSLog (@"String begins with The");
result = [string1 hasSuffix: @"dog"];
if (result)
NSLog (@"String ends with dog");
转换大小写:
• 单词首字母大写:
NSString *string1 = @"The quicK brOwn fox jumpeD";
NSString *string2;
string2 = [string1 capitalizedString];
结果是 "The Quick Brown Fox Jumped" .
• 全部小写
NSString *string1 = @"The quicK brOwn fox jumpeD";
NSString *string2;
string2 = [string1 lowercaseString];
结果是 "the quick brown fox jumped".
• 全部大写
NSString *string1 = @"The quicK brOwn fox jumpeD";
NSString *string2;
string2 = [string1 uppercaseString];
结果是 "THE QUICK BROWN FOX JUMPED".
以上例子改变的都是string2,而string1仍然保持原样不变。
把字符串转成数字类型:
直接看例子就可以明白了:
• 转换为 int
NSString *string1 = @"10";
int myInt = [string1 intValue];
NSLog (@"%i", myInt);
• 转换为 double
NSString *string1 = @"10.1092";
double myDouble = [string1 doubleValue];
NSLog (@"%f", myDouble);
• 转换为 float
NSString *string1 = @"10.1092";
float myFloat = [string1 floatValue];
NSLog (@"%f", myFloat);
• 转换为 NSInteger
NSString *string1 = @"10";
NSInteger myInteger = [string1 integerValue];
NSLog (@"%li", myInteger);
把字符串转换为 ASCII 码:
例子如下:
NSString *string1 = @"The quick browen fox";
const char *utfString = [string1 UTF8String];
printf ("Converted string = %sn", utfString);
0bjective-c 之 NSString 使用详解的更多相关文章
- IOS NSString 用法详解
[cpp] view plain copy //NSString 操作均不改变自身值 //构建字符串 NSString *szTmp = @"A string"; ...
- ios label 自动计算行高详解
在OC当中自动计算行高主要调用系统的 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff } span ...
- NSURLSession详解
导语 现在NSURLConnection在开发中会使用的越来越少,iOS9已经将NSURLConnection废弃,现在最低版本一般适配iOS7,所以也可以使用. NSURLConnection相对于 ...
- NSSearchPathForDirectoriesInDomains函数详解
NSSearchPathForDirectoriesInDomains函数详解 #import "NSString+FilePath.h" @implementation ...
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- 李洪强iOS经典面试题156 - Runtime详解(面试必备)
李洪强iOS经典面试题156 - Runtime详解(面试必备) 一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C ...
- 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)
李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...
- iOS中 HTTP/Socket/TCP/IP通信协议详解
// OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 // 7. ...
- iPhone应用开发 UITableView学习点滴详解
iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...
随机推荐
- select into from和insert into select from两种表复制语句区别
select into from和insert into select from两种表复制语句都是将源表source_table的记录插入到目标表target_table,但两句又有区别. 第一句(s ...
- Android笔记——PreferenceActivity结合PreferenceFragment创建设置界面
1.创建类继承PreferenceActivity 2.该类必须重写OnBuildHeaders和isValidFragment方法. (1)OnBuildHeaders方法中使用lo ...
- word2010中怎样快速修改同级标题格式
我要把所有三级目录的字体增大,怎样能一次选中批量修改?文章很长,一百多个三级标题.word 2010中提供了快速修改的方法: ①将光标定位在一个三级标题中② <IGNORE_JS_OP> ...
- SSIS ->> 生成时间格式
有时我们需要生成一个文件,文件名的构成是当前时间的特定字符串格式,如yyyyMMddHHmmss 当前时间变成yyyyMMddHHmmss这个格式: (DT_WSTR,4) YEAR(GETDATE( ...
- hbase 学习笔记二----shell
Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统. Hbase不同于一般的关系型数据库 ...
- android 设置控件的颜色,字体
1.用代码设置控件的颜色: int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色 mButton.setTextCo ...
- Jalopy 之 HelloWorld —— Jalopy 在 MyEclipse 下的安装与使用
如果你要问我Jalopy是什么.我只能告诉你“它是一个格式化代码的工具”.因为我也是一个初学者. 如果你也是初次接触,那一起来学习下吧! ·安装 1.首先,下载资源 下载地址:http://sourc ...
- [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】
拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...
- make clean vs make clobber
make is pretty smart, and picks up what has changed from the last build, so if you run repo sync and ...
- Volley HTTP库系列教程(2)Volley.newRequestQueue示例,发请求的流程,取消请求
Sending a Simple Request Previous Next This lesson teaches you to Add the INTERNET Permission Use n ...