欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Objective-C中字符串NSString的常用操作方法总结

程序员文章站 2023-12-11 23:04:34
1.创建字符串 (1)常量字符串 复制代码 代码如下: nsstring *string = @"i am an iosdevtip!"; (2)常用创建方法...

1.创建字符串

(1)常量字符串

复制代码 代码如下:

nsstring *string = @"i am an iosdevtip!";

(2)常用创建方法
复制代码 代码如下:

nsstring *string = [[nsstring alloc] init];

string = @"i am an iosdevtip too!";


(3)用initwithstring创建字符串
复制代码 代码如下:

nsstring *string = [[nsstring alloc] initwithstring:@"iosdevtip is here!"];

2.格式化创建字符串

(1)int格式化字符串

复制代码 代码如下:

int age = 20;
nsstring *personage = [nsstring stringwithformat:@"this person age is %d",age];

既然int格式化字符串,那么float、double等,也可以格式化字符串。

(2)nsstring格式化字符串

复制代码 代码如下:

nsstring *name = @"iosdevtip";
nsstring *personname = [nsstring stringwithformat:@"this person name is %@",name];

3.字符串比较

(1)isequaltostring方法比较

复制代码 代码如下:

nsstring *stingone = @"this is an iosdevtip!";
nsstring *stringtwo = @"this is an iosdevtip!";
bool result = [stingone isequaltostring:stringtwo];

(2)compare方法比较
复制代码 代码如下:

bool result = [stingone compare:stringtwo] == nsorderedsame;

compare:方法返回值类型为nscomparisonresult。而nscomparisonresult有下面几个枚举值。
复制代码 代码如下:

typedef ns_enum(nsinteger, nscomparisonresult) {nsorderedascending = -1l, nsorderedsame, nsordereddescending};

4.字符串大小写转换

(1)小写转大写

复制代码 代码如下:

 nsstring *string = @"this is an iosdevtip!";
[string lowercasestring];

(2)大写转小写
复制代码 代码如下:

nsstring *string = @"this is an iosdevtip!";
[string uppercasestring];

(3)首字母大写
复制代码 代码如下:

nsstring *string = @"ligang";
nslog(@"string: %@",[string capitalizedstring]);

打印:
2015-07-16 23:06:11.652 iosstrongdemo[10279:3062010] string: ligang

5.截取字符串

(1)substringtoindex截取字符串

复制代码 代码如下:

nsstring *string = @"this is a operation string!";
nsstring *subtostring = [string substringtoindex:6];

(2)截取的subtostring为this i

substringfromindex截取字符串

复制代码 代码如下:

nsstring *subfromstring = [string substringfromindex:6];

(3)截取的subfromstring为s a operation string!

substringwithrange截取字符串

复制代码 代码如下:

nsstring *rangestring = [string substringwithrange:nsmakerange(6, 3)];

截取的rangestring为s a!

6.判断字符串是否包含另一个字符串

(1)rangeofstring判断

复制代码 代码如下:

nsstring *string1 = @"this is a iosdevtip";
nsstring *string2 = @"iosdevtip";
nsrange range = [string1 rangeofstring:string2];
nsinteger location = range.location;
nsinteger leight = range.length;
nsstring *logstring = [[nsstring alloc] initwithstring:[nsstring stringwithformat:@"location:%ld,leight:%ld",location,leight]];
nslog(@"logstring:%@",logstring);

打印出来:
iosstrongdemo[8837:2221170] logstring:location:10,leight:9

(2)判断是否包含前后缀

复制代码 代码如下:

nsstring *string = @"i love china";
bool ishasi = [string hasprefix:@"i"];
bool ishaschina = [string hassuffix:@"china"];

7.分割字符串

复制代码 代码如下:

nsstring *string = @"this is a iosdevtip";
nsarray *array = [string componentsseparatedbystring:@"a"];
nsstring *string1 = [array objectatindex:0];
nsstring *string2 = [array objectatindex:1];
nslog(@"string1:%@  string2:%@",string1,string2);

打印:
2015-07-16 22:40:39.559 iosstrongdemo[10165:3055448] string1:this is  string2: iosdevtip

8.插入字符串

复制代码 代码如下:

nsmutablestring *string = [[nsmutablestring alloc] initwithstring:@"i china"];
[string insertstring:@"love " atindex:2];
nslog(@"string: %@",string);

打印:
2015-07-16 22:44:10.706 iosstrongdemo[10206:3057014] string: i love china
(1)追加字符串
复制代码 代码如下:

nsmutablestring *string = [[nsmutablestring alloc] initwithstring:@"i love "];
[string appendstring:@"china"];
nslog(@"string:%@",string);

打印:
2015-07-16 22:42:32.305 iosstrongdemo[10189:3056410] string:i love china

9.删除字符串

复制代码 代码如下:

nsmutablestring *string = [[nsmutablestring alloc] initwithstring:@"i love china"];
[string deletecharactersinrange:nsmakerange(2, 4)];
nslog(@"string1: %@",string);

打印:
2015-07-16 22:46:58.437 iosstrongdemo[10219:3057749] string1: i china

10.替换字符串

复制代码 代码如下:

nsstring *string = @"i love china";
nsstring *replacestring = [string stringbyreplacingoccurrencesofstring:@"love" withstring:@"like"];
nslog(@"replacestring:  %@",replacestring);

打印:
2015-07-16 22:56:07.405 iosstrongdemo[10236:3059503] replacestring: i like china

11.去除字符串首尾的空格和换行符

复制代码 代码如下:

nsstring *string = @" i love china ";
nsstring *text = [string stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]];
nslog(@"text:%@",text);

打印:
2015-07-16 23:00:47.845 iosstrongdemo[10265:3061013] text:i love china

上一篇:

下一篇: