`

NSString+NSMutableString+NSValue+NSAraay用法汇总

阅读更多

开发过程中难免遇到字符串操作,下面是CocoaChina为您总结的NSString+NSMutableString+NSValue+NSAraay用法汇总,帮您应对各种字符串操作。

//一、NSString
/*----------------创建字符串的方法----------------*/

//1、创建常量字符串。
NSString *astring = @"This is a String!";


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

NSString *astring = [[NSString alloc] init];
astring = @"This is a String!";
NSLog(@"astring:%@",astring);
[astring release];




//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);




/*----------------从文件读取字符串:initWithContentsOfFile方法 ----------------*/

NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];


/*----------------写字符串到文件:writeToFile方法 ----------------*/


NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";
[astring writeToFile: path atomically: YES];
[astring release];




/*---------------- 比较两个字符串----------------*/

//用C比较:strcmp函数

char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0)
{
NSLog(@"1");
}



//isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);




//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 compare:astring02] = = NSOrderedDescending;
NSLog(@"result:%d",result);
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)



//不考虑大 小写比较字符串1
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);

//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];


/*----------------抽取子串 ----------------*/

//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);




//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);




//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);

//扩展路径

NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);



//文件扩展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);




/*******************************************************************************************
NSMutableString
*******************************************************************************************/

/*---------------给字符串分配容量----------------*/
//stringWithCapacity:
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];


/*---------------在已有字符串后面添加字符----------------*/

//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);
*/


/*-------- 在已有字符串中按照所给出范围和长度删除字符------*/
/*
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"String1:%@",String1);



/*--------在已有字符串后面在所指定的位置中插入给出的字符串------*/

//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);



/*--------将已有的空符串换成其它的字符串------*/

//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);



/*--------按照所给出的范围,和字符串替换的原有的字符------*/

//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
NSLog(@"String1:%@",String1);



/*-------------判断字符串内是否还包含别的字符串(前缀,后缀)-------------*/
//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,这一点前面在串中搜索子串用到过;



/*******************************************************************************************
NSArray
*******************************************************************************************/

/*---------------------------创建数组 ------------------------------*/
//NSArray *array = [[NSArray alloc] initWithObjects:
@"One",@"Two",@"Three",@"Four",nil];

self.dataArray = array;
[array release];

//- (unsigned) Count;数组所包含对象个数;
NSLog(@"self.dataArray cound:%d",[self.dataArray count]);

//- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);


/*-------------------------- 从一个数组拷贝数据到另一数组(可变数级)----------------------------*/

//arrayWithArray:
//NSArray *array1 = [[NSArray alloc] init];
NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
NSArray *array = [NSArray arrayWithObjects:
@"a",@"b",@"c",nil];
NSLog(@"array:%@",array);
MutableArray = [NSMutableArray arrayWithArray:array];
NSLog(@"MutableArray:%@",MutableArray);

array1 = [NSArray arrayWithArray:array];
NSLog(@"array1:%@",array1);


//Copy

//id obj;
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

NSLog(@"oldArray:%@",oldArray);
for(int i = 0; i < [oldArray count]; i++)
{
obj = [[oldArray objectAtIndex:i] copy];
[newArray addObject: obj];
}
//
NSLog(@"newArray:%@", newArray);
[newArray release];


//快速枚举

//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);

for(id obj in oldArray)
{
[newArray addObject: obj];
}
//
NSLog(@"newArray:%@", newArray);
[newArray release];


//Deep copy

//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
NSLog(@"newArray:%@", newArray);
[newArray release];


//Copy and sort

//NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:

@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
NSLog(@"oldArray:%@",oldArray);
NSEnumerator *enumerator;
enumerator = [oldArray objectEnumerator];
id obj;
while(obj = [enumerator nextObject])
{
[newArray addObject: obj];
}
[newArray sortUsingSelector:@selector(compare:)];
NSLog(@"newArray:%@", newArray);
[newArray release];



/*--------------------------- 切分数组------------------------------*/

//从字符串分割到数组- componentsSeparatedByString:
NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];


//从数组合并元素到字符串- componentsJoinedByString:
NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);



/*******************************************************************************************
NSMutableArray
*******************************************************************************************/
/*--------------- 给数组分配容量----------------*/
//NSArray *array;
array = [NSMutableArray arrayWithCapacity:20];



/*-------------- 在数组末尾添加对象----------------*/
//- (void) addObject: (id) anObject;
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
[array addObject:@"Four"];
NSLog(@"array:%@",array);



/*-------------- 删除数组中指定索引处对象----------------*/
//-(void) removeObjectAtIndex: (unsigned) index;
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
[array removeObjectAtIndex:1];
NSLog(@"array:%@",array);



/*------------- 数组枚举---------------*/
//- (NSEnumerator *)objectEnumerator;从前向后
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
NSEnumerator *enumerator;
enumerator = [array objectEnumerator];

id thingie;
while (thingie = [enumerator nextObject]) {
NSLog(@"thingie:%@",thingie);
}


//- (NSEnumerator *)reverseObjectEnumerator;从后向前
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
NSEnumerator *enumerator;
enumerator = [array reverseObjectEnumerator];

id object;
while (object = [enumerator nextObject]) {
NSLog(@"object:%@",object);
}


//快速枚举
//NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"One",@"Two",@"Three",nil];
for(NSString *string in array)
{
NSLog(@"string:%@",string);
}



/*******************************************************************************************
NSDictionary
*******************************************************************************************/

/*------------------------------------创建字典 ------------------------------------*/
//- (id) initWithObjectsAndKeys;

//NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
NSString *string = [dictionary objectForKey:@"One"];
NSLog(@"string:%@",string);
NSLog(@"dictionary:%@",dictionary);
[dictionary release];


/*******************************************************************************************
NSMutableDictionary
*******************************************************************************************/

/*------------------------------------创建可变字典 ------------------------------------*/
//创建
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

//添加字典
[dictionary setObject:@"One" forKey:@"1"];
[dictionary setObject:@"Two" forKey:@"2"];
[dictionary setObject:@"Three" forKey:@"3"];
[dictionary setObject:@"Four" forKey:@"4"];
NSLog(@"dictionary:%@",dictionary);

//删除指定的字典
[dictionary removeObjectForKey:@"3"];
NSLog(@"dictionary:%@",dictionary);


/*******************************************************************************************
NSValue(对任何对象进行包装)
*******************************************************************************************/

/*--------------------------------将NSRect放入NSArray中 ------------------------------------*/
//将NSRect放入NSArray中
NSMutableArray *array = [[NSMutableArray alloc] init];
NSValue *value;
CGRect rect = CGRectMake(0, 0, 320, 480);
value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
[array addObject:value];
NSLog(@"array:%@",array);

//从Array中 提取
value = [array objectAtIndex:0];
[value getValue:&rect];
NSLog(@"value:%@",value);


/*******************************************************************************************
从目录搜索扩展名为jpg的文件
*******************************************************************************************/

//NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *home;
home = @"../Users/";

NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: home];

NSMutableArray *files = [[NSMutableArray alloc] init];

//枚举
NSString *filename;
while (filename = [direnum nextObject]) {
if([[filename pathExtension] hasSuffix:@"jpg"]){
[files addObject:filename];
}
}

//快速枚举
//for(NSString *filename in direnum)
//{
//if([[filename pathExtension] isEqualToString:@"jpg"]){
//[files addObject:filename];
//}
//}
NSLog(@"files:%@",files);

//枚举
NSEnumerator *filenum;
filenum = [files objectEnumerator];
while (filename = [filenum nextObject]) {
NSLog(@"filename:%@",filename);
}

分享到:
评论

相关推荐

    NSString+Hashing.m

    md5加密代码详细的加文件NSString+Hashing.m

    NSString+FileSize.zip

    使用`NSString+FileSize`扩展时,我们只需提供文件或目录的完整路径,例如`/Documents/file.txt`,然后调用`fileSize`方法。示例代码如下: ```objc NSString *filePath = @"path/to/your/file"; NSNumber *...

    NSString+Hash(Objective-C)

    Objective-C 中 NSString 关于常见 Hash 算法的分类,能对字符串,文件进行 Hash 运算,具备 HMAC 功能。支持的 Hash 算法有:MD5、SHA1、SHA256、SHA512。

    NSString_NSMutableString

    Objective-C里核心的处理字符串的类就是NSString和NSMutableString这两个类,这两个类完成了Objective-C中字符串大部分功能的处理。这两个类的最主要的区别是NSString创建赋值后不能动态修改长度和内容,除非给重新...

    NSString+ToolString

    `NSString+ToolString` 可能会提供一个`isPhoneNumber` 方法,该方法使用正则表达式检查输入的字符串是否符合中国手机号的格式。 2. **银行卡号验证**:银行卡号通常由16-19位数字组成,不同的银行有不同的校验规则...

    NSString+ReplaceUnicode

    通过引入这个类别,我们可以在任何使用NSString的地方调用`replaceUnicodeWithChinese`方法,快速处理Unicode编码的中文字符串。 总之,"NSString+ReplaceUnicode"是一个实用的工具,它增强了NSString类的能力,...

    NSString+Extended

    `NSString+Extended`通常指的是开发者为了增加NSString的功能而创建的一个类别(Category),它扩展了NSString的原有功能,提供了更多的便利方法。这个扩展可能包含了对字符串的各种实用操作,比如格式化、拼接、...

    OC Extension NSString+FHXExtension(字符串扩展).zip

    /** * 返回文字的size * * @param font 文字大小 * * @param maxSize 限制的宽高 * * @return 返回Size */ ...+ (UIViewController*)stringChangeToClass:(NSString *)str; /** * MD5加密

    NSString-BlockHelper:NSString + BlockHelper

    NSString-BlockHelper NSString + BlockHelper 例子 NSString *word1 = @"word1"; NSString *word2 = @"word2"; NSString *word3 = word1.add(@" ").add(word2); NSLog(@"%@",word3); //output : word1 ...

    NSData+AESCrypt库

    - (NSData *)AES256EncryptWithKey:(NSString *)key; - (NSData *)AES256DecryptWithKey:(NSString *)key; + (NSData *)dataWithBase64EncodedString:(NSString *)string; - (id)initWithBase64EncodedString:...

    iOS的MD5加密

    在iOS开发中,数据安全是至关重要的一个环节,而MD5(Message-Digest Algorithm 5)是一种广泛使用的哈希函数,常用于数据的完整性校验和密码存储。本篇文章将详细解析标题“iOS的MD5加密”所涉及的知识点,并结合...

    iOS MD5加密

    在提供的文件列表中,`NSString+Hash.h`和`NSString+Hash.m`是两个Objective-C类别文件,它们可能扩展了`NSString`类,添加了MD5加密的方法。通常,这样的类别会包含如`- (NSString *)md5String`这样的方法,允许...

    NSString-QualifierSearchParser:NSString 类别,用于使用限定符解析搜索查询,如 github 代码搜索或 gmail 过滤

    NSString 类别,用于使用限定符(如 github 代码搜索或 gmail 过滤)解析搜索查询。 从NSString将创建一个NSDictionary其中包含为给定限定符找到的值。 接受以下形式的查询: qualifier1:value qualifier2:value ...

    NSString扩展类

    这个“NSString扩展类”是为了简化开发者的工作,将常见的字符串处理方法进行了封装,使得在处理复杂字符串任务时可以更高效、简洁。下面我们将详细探讨这个扩展类包含的知识点。 首先,`NSString+Addtions.h`文件...

    深拷贝与浅拷贝;copy与mutableCopy;容器类型的深拷贝;copy和strong;

    3、无论是声明NSString还是NSMutableString类型的属性时,我们希望此属性被赋值为NSMutableString类型的字符串后,此属性不会因这个可变类型字符串的改变而改变(这也是多数情况下的用法),那就用copy修饰属性

    NSString-TextAndNumberValidation:验证Objective-C中流行的标准

    NSString类别方法可简化流行的字符串标准验证 验证: 波兰语PESEL号码(个人号码) 波兰NIP号码 波兰REGON编号 使用正则表达式的电子邮件地址 信用卡号码-Luhn号码 银行帐号 示例项目 示例项目显示了如何使用这些...

    NSString扩展

    - **其他扩展**:对`NSMutableString`也进行了扩展,添加了`deleteLastCharacter`方法来删除字符串末尾的一个字符。 ### 总结 通过对`NSString`的扩展,不仅增强了字符串处理的功能,还提高了代码的可读性和重用性...

    Lecture 3: Objective-C

    本讲座深入探讨了Objective-C编程语言的关键概念,包括方法(类和实例)、实例变量、属性、动态绑定、内省、nil与BOOL,以及Foundation框架中的核心类如NSObject、NSString、NSMutableString、NSNumber、NSValue、...

    日历-iOS:日历视图

    有适用于iPhone和iPad的示例,以及使用弹出框的示例。 使用弹出框: 如何安装 手册: 将下一个源文件添加到您的项目中: CalendarView.h CalendarView.m NSDate+CalendarView.h NSDate+CalendarView.m NSString+...

    NSString-Emoji:NSString (Emoji) 扩展了 NSString 类以提供与 Emoji 表情相关的自定义功能

    NSString+表情符号 入门 NSString (Emoji) 扩展了 NSString 类以提供与 Emoji 表情符号相关的自定义功能。 通过此类别,可以将转换为 unicode 表情符号字符,反之亦然(如果您需要将用户键入的消息发布到远程服务,...

Global site tag (gtag.js) - Google Analytics