//一、NSString

//1、创建常量字符串。

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

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

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

astring = @"This is a String!";

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

NSString *path = @"astring.text";

NSString *astring = [[NSString alloc]

initWithContentsOfFile:path];

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

[astring release];

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

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

*/

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

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

}

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

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

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

}

//快速枚举

//for(id object in files)

//{

//NSLog(@"object:%@",object);

//}

ios html数组,[转载]IOS中字符串与数组的处理相关推荐

  1. php 中的字符串转数组函数,php中字符串转数组的函数是什么

    php中将字符串转换为数组的函数有:1.str_split()函数,语法"str_split(string,length)":2.explode()函数,可返回字符串数组:3.pr ...

  2. java字符串数组转json_java中字符串String格式转化成json格式

    java字符串数组转json_java中字符串String格式转化成json格式 String s= Connection.deleteHost("10310");System.o ...

  3. ES6中字符串和数组新增的方法

    ES6中字符串和数组新增的方法 一.字符串中新增的方法 1.模板字符串 (表达式.函数的调用.变量) 2.repeat(次数)函数 : 将目标字符串重复N次,返回一个新的字符串,不影响目标字符串 3. ...

  4. js中字符串与数组之间的转换

    本文实例介绍了javascript中字符串和数组的相互转换方法,分享给大家供大家参考,具体内容如下 字符串和数组的相互转换操作是非常的重要的,因为在实际编码过程中会经常用到,所以这是必须要掌握的知识点 ...

  5. Object-c 中字符串与数组的处理

    //一.NSString /*----------------创建字符串的方法----------------*/   //1.创建常量字符串. NSString *astring = @" ...

  6. 小程序中字符串转数组如何实现

    JS 字符串转数组: var words =' likely, likely, likely,economic, economic';     //字符串中间已特殊符号隔开 var words = w ...

  7. java中字符串和数组如何比较_[Java教程]javascript中数组和字符串的方法比较

    [Java教程]javascript中数组和字符串的方法比较 0 2016-07-19 23:00:05 ×目录[1]可索引 [2]转换 [3]拼接[4]创建[5]位置 前面的话 字符串和数组有很多的 ...

  8. java中字符串转数组,数组转字符串,集合转字符串

    一.字符串转数组 这里使用的是split()方法. String ids = "1,2,3,4"; String[] idArray = ids.split(",&quo ...

  9. bash中字符串和数组的遍历

    让我们看如下的bash script A="china fengxi zhongguo" echo "echo varialbe A directly:" $A ...

最新文章

  1. openStack调试
  2. 浏览器渲染原理及可能出现的bug
  3. MySQL学习(四)查询
  4. 08-GCD常用方法
  5. Oracle9i卸载后再次安装,设置的SID相同出现“指定的SID在本机上已经存在。请指定一个不同的SID。”...
  6. ReentrantLock可重入锁使用及原理
  7. 首次push本地代码到github上出现的问题及解决方案
  8. matlab图像处理 推荐,Matlab计算机视觉、图像处理工具箱推荐
  9. python网络编程知识_python六十七课——网络编程(基础知识了解)
  10. 概率论:集合、假设检验、分布、矩阵、估计
  11. 舞台音效控制软件_iSurround(电脑音效调节器) V1.0.0.1 官方版
  12. iText5官方系列教程-iText in Action(一)
  13. 毕业设计 嵌入式太空游戏机设计与实现
  14. 计算机设计大赛一人能报几个,我校在中国大学生计算机设计大赛获多个奖项
  15. 2019.02.28 bzoj4199: [Noi2015]品酒大会(sam+线段树)
  16. 尚德机构季报图解:净利1.79亿 实现连续四个季度盈利
  17. 【剑指Offer】整数(一)整数除法 - 两数相除 - JavaScript
  18. 用python绘制熊猫图案_python – 熊猫:如何在彼此之上绘制年度数据
  19. mysql表analyze,ANALYZE TABLE语句如何帮助维护MySQL表?
  20. 服务器安装内置无线网卡,笔记本怎么安装内置网卡 笔记本内置无线网卡安装步骤【图文】...

热门文章

  1. JS 前端 判断字符串是否相等
  2. Slater条件理解和证明
  3. UG NX 10 打开STP装配体的解决方法
  4. intel PFR功能
  5. Apriori算法简介---关联规则的频繁项集算法
  6. ES数据迁移_snapshot(不需要安装其他软件)
  7. 独立思考模型与经验-仅供参考
  8. 使用Swiper4插件问题
  9. 六、Spring Boot - 自动配置原理(2)
  10. YEN--K最短路算法(K-Shortest-Path) Java实现