您的位置:首页 > 其它

NSString、NSMutableString基本用法

2016-02-25 09:25 330 查看

Demo

main.m文件

//

//  main.m

//  11111

//

//  Created by apple on 15/8/17.

//  Copyright (c) 2015年 apple. All rights reserved.

//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

@autoreleasepool {

#if 0   //输出1

// 字符串比较大小的规则

//以字符是ASCII值确定,比较规则是,从第一个字符开始,顺次向后直到出现不同的字符为止,然后以第一个不同的字符的ASCII值确定,例如上面的”abc”和"aabdfg",由于第一个字符相同,都是'a'所以看下一个字符,第二个字符,一个是'b',一个是‘a',由于b的ASCII值比a的ASCII值大,所以,这二个字符串的比较结果是"abc">"aabdfg"

NSString *string = @"ab";

NSString *string1 = @"ab";

NSString *string2 = @"abc";

//比较两个字符串的大小

NSComparisonResult result = [string compare:string2];

NSLog(@"%ld",(long)result);//NSOrderedAscending升序(-1)

NSComparisonResult result1 = [string1 compare:string];

NSLog(@"%ld",(long)result1);//NSOrderedSame相等(0)

NSComparisonResult result2 = [string2 compare:string];

NSLog(@"%ld",(long)result2);//NSOrderedDescending降序(1)

NSLog(@"---> %p",string);//NSString为不可变对象,故地址相同

NSLog(@"---> %p",string1);

NSLog(@"str长度--->%ld",[string length]);//不包含结束符‘\n’

NSLog(@"str2长度--->%ld",[string2 length]);

NSLog(@"---------1----------");

NSString *string3 = @"这是一个小测试...";

//substringFromIndex:表示从指定位置开始截取字符串到最后,所截取位置包含该指定位置。包含指定位置字符

NSString *tmpStr = [string3 substringFromIndex:2];

NSLog(@"%@",tmpStr);

//substringToIndex:表示从第一个字符串开始截取,到指定长度位置,但是不包括该指定位置位置字符串

tmpStr = [string3 substringToIndex:2];

NSLog(@"%@",tmpStr);

//substringWithRange:需要传进来NSRange类型,表示从哪个下标开始截取和截取长度,返回字符串类型,包含下标字符

tmpStr = [string3 substringWithRange:NSMakeRange(1, 2)];

NSLog(@"%@",tmpStr);

NSLog(@"---------2----------");

//        NSString *string4 = @"hello";

//        NSString *string5 = @"world";

//compare 比较规则options

NSString
13d2f
*string4 = @"abcdefg";

NSString *string5 = @"ABcdee";

NSString *string6 = @"abcdee";

// NSLiteralSearch 区分大小写(完全比较)

NSComparisonResult result6 = [string4 compare:string5 options:NSLiteralSearch];

NSLog(@"%ld",(long)result6);//降序1

// NSCaseInsensitiveSearch 不区分大小写

NSComparisonResult result4 = [string6 compare:string5 options:NSCaseInsensitiveSearch];

NSLog(@"%ld",(long)result4);//相等0

// NSNumericSearch 只比较字符串的个数,而不比较字符串的字面值

NSComparisonResult result5 = [string4 compare:string5 options:NSNumericSearch];

NSLog(@"%ld",(long)result5);//降序1

#endif

#if 0   //输出2

NSLog(@"---------3----------");

//比较字符串是否相等

NSString *string1 = @"ab";

NSString *string2 = @"abc";

//if(string == string2){

if([string1 isEqualToString:string2]){

NSLog(@"二者相等");

}else{

NSLog(@"二者不相等");

}

//搜索字符串

//字符串开头是否包括另一字符串 hasPrefix,BOOL时返回结果YES(true)

NSString *demoString = @"www.apple.com";

if([demoString hasPrefix:@"www.apple"]){

NSLog(@"have it");

}else{

NSLog(@"oh shit");

}

// 字符串结尾是否包括另一字符串 hasSuffix,BOOL时返回结果YES(true)

if([demoString hasSuffix:@"e.com"]){

NSLog(@"是e.com结尾");

}else{

NSLog(@"不是e.com结尾");

}

// 字符串是否包括另一字符子串 containsString

if([demoString containsString:@"appl"]){

NSLog(@"包含appl这个子串");

}else{

NSLog(@"不包含appl这个子串");

}

NSLog(@"---------4----------");

//rangeOfString 前面的参数是要被搜索的字符串,后面的是要搜索的字符

//例if ([str1 rangeOfString:str].location != NSNotFound)  {  NSLog(@"这个字符串中有\n"); }// 其中NSNotFound 表示请求操作的某个内容或者item没有发现,或者不存在

NSRange resultRange = [demoString rangeOfString:@"apple"];

//打印被搜索字符的位置和长度(不含结束符)

NSLog(@"location=%lu,length=%lu",(unsigned long)resultRange.location,(unsigned long)resultRange.length);

NSRange resultRange1 = [demoString rangeOfString:@"appw"];

NSLog(@"location=%lu,length=%lu",(unsigned long)resultRange1.location,(unsigned long)resultRange1.length);

NSLog(@"---------5----------");

if ([demoString rangeOfString:@"appw"].location != NSNotFound)  {

NSLog(@"这个字符串中有appw");

}else {

NSLog(@"这个字符串中没有appw");

}

#endif

#if 0   //输出3

NSLog(@"-----------4----------");

//stringWithFormat可以格式化输入字符串,便于将其他类型转化为字符串类型。

//在OC中stringWithFormat会新申请一片空间并初始化字符串,所以每一个用stringWithFormat方法得到的字符串其指针都是不相同的

//integerValue整型变量值

NSString *numString = @"55";

NSInteger num1 = numString.integerValue;//字符串->数字

NSLog(@"%ld",(long)num1);

NSNumber *num2 = @22;

NSString *toString = [NSString stringWithFormat:@"%@",num2];//数字->字符串

NSLog(@"%@",toString);

//initWithFormat是字符串的初始化方法,这个方法的目的是按照第一个参数中所给的格式化字符串来拼装一个完整的字符串

NSString *string1 = @"ab";

NSString *string2 = @"bc";

NSString *stringHW = [NSString stringWithFormat:@"%@ love %@",string1,string2];

NSLog(@"%@",stringHW);

NSString *strS = [[NSString alloc]initWithFormat:@"lol"];

NSLog(@"%@",strS);

NSLog(@"---->%@",[@"world" uppercaseString]);//小写->大写

NSLog(@"---->%@",[@"HELLO" lowercaseString]);//大写->小写

NSLog(@"---->%@",[@"welcome to home" capitalizedString]);//返回字符串,串中的每个单词的首字母大写,其余字母小写

#endif

#if 0   //输出4

NSLog(@"-----------5----------");

const char *c_str = "hello world C";

//c转oc

NSString *c_ocString =  [NSString stringWithUTF8String:c_str];

NSLog(@"%@",c_ocString);

//oc转c

NSLog(@"%s",[@"hello world OC" cStringUsingEncoding:NSUTF8StringEncoding]);

#endif

#if 0   //输出5

NSLog(@"-----------6----------");

NSString *filePath = @"/Users/apple/Desktop/文档保存/demoFile";

NSError *error;

NSString *contents = @"这是一个文本测试啊啊啊啊啊!!!";

//把字符串写入本地文件

[contents writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

NSLog(@"error:%@",[error localizedDescription]);

#endif

#if 0    //输出6

NSString *filePath = @"/Users/apple/Desktop/文档保存/demoFile";

NSError *error;

//把本地文件读入到内存

NSString *contentsString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

//打印错误和文件内容

NSLog(@"error:%@",[error localizedDescription]);

NSLog(@"contentsString:%@",contentsString);

#endif

#if 0   //输出7

NSString *path = @"/Users/apple/Desktop/文档保存/ppp";

NSError *error;

//直接把字符串写入本地文件

[@"demoString--->" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

NSLog(@"error:%@",[error localizedDescription]);

#endif

// Do any additional setup after loading the view, typically from a nib.

}

return 0;

}

输出1:

2015-09-09 11:38:58.795 11111[892:48564] -1

2015-09-09 11:38:58.797 11111[892:48564] 0

2015-09-09 11:38:58.797 11111[892:48564] 1

2015-09-09 11:38:58.797 11111[892:48564] ---> 0x100001068

2015-09-09 11:38:58.797 11111[892:48564] ---> 0x100001068

2015-09-09 11:38:58.797 11111[892:48564] str长度--->2

2015-09-09 11:38:58.797 11111[892:48564] str2长度--->3

2015-09-09 11:38:58.798 11111[892:48564] ---------1----------

2015-09-09 11:38:58.798 11111[892:48564] 一个小测试...

2015-09-09 11:38:58.799 11111[892:48564] 这是

2015-09-09 11:38:58.799 11111[892:48564] 是一

2015-09-09 11:38:58.800 11111[892:48564] ---------2----------

2015-09-09 11:38:58.800 11111[892:48564] 1

2015-09-09 11:38:58.800 11111[892:48564] 0

2015-09-09 11:38:58.800 11111[892:48564] 1

输出2:

2015-09-09 11:52:57.047 11111[912:50684] ---------3----------

2015-09-09 11:52:57.049 11111[912:50684] 二者不相等

2015-09-09 11:52:57.049 11111[912:50684] have it

2015-09-09 11:52:57.049 11111[912:50684] 是e.com结尾

2015-09-09 11:52:57.049 11111[912:50684] 包含appl这个子串

2015-09-09 11:52:57.049 11111[912:50684] ---------4----------

2015-09-09 11:52:57.049 11111[912:50684] location=4,length=5

2015-09-09 11:52:57.049 11111[912:50684] location=9223372036854775807,length=0

2015-09-09 11:52:57.050 11111[912:50684] ---------5----------

2015-09-09 11:52:57.050 11111[912:50684] 这个字符串中没有appw

输出3:

2015-09-09 12:01:39.933 11111[930:52009] -----------4----------

2015-09-09 12:01:39.935 11111[930:52009] 55

2015-09-09 12:01:39.936 11111[930:52009] 22

2015-09-09 12:01:39.936 11111[930:52009] ab love bc

2015-09-09 12:01:39.936 11111[930:52009] lol

2015-09-09 12:01:39.937 11111[930:52009] ---->WORLD

2015-09-09 12:01:39.938 11111[930:52009] ---->hello

2015-09-09 12:01:39.938 11111[930:52009] ---->Welcome To Home

输出4:

2015-09-09 12:02:47.738 11111[939:52424] -----------5----------

2015-09-09 12:02:47.739 11111[939:52424] hello world C

2015-09-09 12:02:47.740 11111[939:52424] hello world OC

输出5:

2015-09-09 16:35:00.847 11111[431:15385] -----------6----------

2015-09-09 16:35:00.850 11111[431:15385] error:(null)

输出6:

2015-09-09 16:38:50.629 11111[452:16465] error:(null)

2015-09-09 16:38:50.630 11111[452:16465] contentsString:这是一个文本测试啊啊啊啊啊!!!

输出7:

2015-09-09 16:46:50.808 11111[497:18433] error:(null)


NSString基本用法

NSString其实是一个对象类型。NSString是NSObject(Cocoa Foundation的基础对象)的子类

一、NSString的创建

1、创建常量字符串。

NSString *astring = @”This is a String!”;

2、创建空字符串,给予赋值。

NSString *astring = [[NSString alloc] init];

astring = @”This is a String!”;

[astring release];

NSLog(@”astring:%@”,astring);

NSString *astring = [[NSString alloc] init];

NSLog(@”0x%.8x”, astring);

astring=@”This is a String!”;

NSLog(@”0x%.8x”, astring);

[astring release];

NSLog(@”astring:%@”,astring);

3、在以上方法中,提升速度:initWithString方法

NSString *astring = [[NSString alloc] initWithString:@”This is a String!”];

NSLog(@”astring:%@”,astring);

[astring release];

4、用标准c创建字符串:initWithCString方法

char *Cstring = “This is a String!”;

NSString *astring = [[NSString alloc] initWithCString:Cstring];

NSLog(@”astring:%@”,astring);

[astring release];

5、创建格式化字符串:占位符(由一个%加一个字符组成)

int i = 1;

int j = 2;

NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@”%d.This is %i string!”,i,j]];

NSLog(@”astring:%@”,astring);

[astring release];

6、创建临时字符串

NSString *astring;

astring = [NSString stringWithCString:”This is a temporary string”];

NSLog(@”astring:%@”,astring);

7、写字符串到文件:writeToFile方法

NSString *astring = [[NSString alloc] initWithString:@”This is a String!”];

NSLog(@”astring:%@”,astring);

NSString *path = @”astring.text”;

[astring writeToFile: path atomically: YES];

[astring release];

8、从文件读取字符串:initWithContentsOfFile方法

NSString *path = @”astring.text”;

NSString *astring = [[NSString alloc] initWithContentsOfFile:path];

NSLog(@”astring:%@”,astring);

[astring release];

二、字符串的比较

1、用C比较:strcmp函数

char string1[] = “string!”;

char string2[] = “string!”;

if(strcmp(string1, string2) = = 0)

{

NSLog(@”1”);

}

2、isEqualToString方法

NSString *astring01 = @”This is a String!”;

NSString *astring02 = @”This is a String!”;

BOOL result = [astring01 isEqualToString:astring02];

NSLog(@”result:%d”,result);

3、compare方法 判断比较(comparer返回的三种值)

NSString *astring01 = @”This is a String!”;

NSString *astring02 = @”This is a String!”;

BOOL result = [astring01 compare:astring02] = = NSOrderedSame;

NSLog(@”result:%d”,result);

//NSOrderedSame判断两者内容是否相同

NSString *astring01 = @”This is a String!”;

NSString *astring02 = @”this is a String!”;

BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;

NSLog(@”result:%d”,result);

//NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)

不考虑大小写比较字符串

NSString *astring01 = @”this is a String!”;

NSString *astring02 = @”This is a String!”;

BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;

NSLog(@”result:%d”,result);

//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)

不考虑大小写比较字符串2

NSString *astring01 = @”this is a String!”;

NSString *astring02 = @”This is a String!”;

BOOL result = [astring01 compare:astring02

options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;

NSLog(@”result:%d”,result);

P.S : NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。

三、改写字符串

NSString *string1 = @”A String”;

NSString *string2 = @”String”;

NSLog(@”string1:%@”,[string1 uppercaseString]);//大写

NSLog(@”string2:%@”,[string2 lowercaseString]);//小写

NSLog(@”string2:%@”,[string2 capitalizedString]);//首字母大小

四、搜索字符串

NSString *string1 = @”This is a string”;

NSString *string2 = @”string”;

NSRange range = [string1 rangeOfString:string2];

int location = range.location;

int leight = range.length;

NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@”Location:%i,Leight:%i”,location,leight]];

NSLog(@”astring:%@”,astring);

[astring release];

五、字符串的截取

1.-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符

NSString *string1 = @”This is a string”;

NSString *string2 = [string1 substringToIndex:3];

NSLog(@”string2:%@”,string2);

2.-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符

NSString *string1 = @”This is a string”;

NSString *string2 = [string1 substringFromIndex:3];

NSLog(@”string2:%@”,string2);

3.-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串

NSString *string1 = @”This is a string”;

NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];

NSLog(@”string2:%@”,string2);

4.截取NSString最后一位符号后的东西

方法1.

NSString str = @”/Users/yangiori/Library/Application Support/iPhone Simulator/5.1/Applicati****/8724956B-407E-4ACD-BBA6-95C7D033C33D/Documents/content/chapters/8”;

NSString *temp1 = [[str componentsSeparatedByString:@”/”] lastObject];

NSLog(@”%@”,temp1);

结果:8

方法2.

NSString str = @”/Users/yangiori/Library/Application Support/iPhone Simulator/5.1/Applicati****/8724956B-407E-4ACD-BBA6-95C7D033C33D/Documents/content/chapters/8”;

NSString *temp2 = [str substringFromIndex:[str length]-1];

NSLog(@”%@”,temp2);

结果:8

5.从指定位置截取字符串

NSString * str =[NSString stringWithFormat:@”********************Documents/image%i.jpg”,2];

NSRange range = [str rangeOfString:@”Documents”];

NSString * result = [str substringFromIndex:range.location];

NSLog(@”%@”,result);

六、其他操作

1.扩展路径

NSString *Path = @”~/NSData.txt”;

NSString *absolutePath = [Path stringByExpandingTildeInPath];

NSLog(@”absolutePath:%@”,absolutePath);

NSLog(@”Path:%@”,[absolutePath stringByAbbreviatingWithTildeInPath]);

2.文件扩展名

NSString *Path = @”~/NSData.txt”;

NSLog(@”Extension:%@”,[Path pathExtension]);

NSMutableString基本用法

1.给字符串分配容量

stringWithCapacity:

NSMutableString *String;

String = [NSMutableString stringWithCapacity:40];

2.在已有字符串后面添加字符

appendString: and appendFormat:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@”This is a NSMutableString”];

[String1 appendString:@”, I will be adding some character”];

[String1 appendFormat:[NSString stringWithFormat:@”, I will be adding some character”]];

NSLog(@”String1:%@”,String1);

3.在已有字符串中按照所给出范围和长度删除字符

deleteCharactersInRange:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@”This is a NSMutableString”];

[String1 deleteCharactersInRange:NSMakeRange(0, 5)];

NSLog(@”String1:%@”,String1);

4.在已有字符串后面在所指定的位置中插入给出的字符串

-insertString: atIndex:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@”This is a NSMutableString”];

[String1 insertString:@”Hi! ” atIndex:0];

NSLog(@”String1:%@”,String1);

5.将已有的空符串换成其它的字符串

-setString:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@”This is a NSMutableString”];

[String1 setString:@”Hello Word!”];

NSLog(@”String1:%@”,String1);

6.按照所给出的范围,和字符串替换的原有的字符

-setString:

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@”This is a NSMutableString”];

[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@”That”];

NSLog(@”String1:%@”,String1);

7.判断字符串内是否还包含别的字符串(前缀,后缀)

01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;

NSString *String1 = @”NSStringInformation.txt”;

[String1 hasPrefix:@”NSString”] = = 1 ? NSLog(@”YES”) : NSLog(@”NO”);

[String1 hasSuffix:@”.txt”] = = 1 ? NSLog(@”YES”) : NSLog(@”NO”);

02:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过;

参考自http://seven-sally.lofter.com/post/19d861_500373
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: