`

Object-c 字符串处理及建立(包含一些数组的使用等)

 
阅读更多

1

//创建字符串对象数组
NSArray *array = [str componentsSeparatedByString:@"@"];//就是以@为标示 输出看看啦


int count=[array count];
int i;
for(i=0;i<count;i=i+4)
{
printf("%i: %s\n",i,[[array objectAtIndex:i] UTF8String]);
}

2 可变的字符串类
NSMutableString *song=[[NSMutableString alloc] init];
[song appendString:@"Deaf Leppard"];
printf("%s\n",[song UTF8String]);

NSRange range=[song rangeOfString:@"Deaf"];//获取字符串"Deaf"字串的范围
[song replaceCharactersInRange:range withString:@"Def"];//替换
printf("%s\n",[song UTF8String]);

[song insertString:@"Animal by " atIndex:0];
printf("%s\n",[song UTF8String]);

[song release];

3
字典加数组操作
NSArray *keys=[@"one two three" componentsSeparatedByString:@" "];
NSArray *value=[@"two bravo a" componentsSeparatedByString:@" "];
NSDictionary *dic=[[NSDictionary alloc] initWithObjects:value forKeys:keys];
printf("%s\n",[[dic description] UTF8String]);

//一、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);
//}
分享到:
评论

相关推荐

    powerbuilder

    由于PowerBuilder使用数值0(即ASCII码为0的字符)结束每个字符串,因此,如果打印控制序列中包含了数值0,应用程序需要使用其它字符在参数string中替代数值0,并用zerochar参数指明这个替代字符。一般来说,应该...

    springmybatis

    其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...

    《Objective-C2.0程序设计(原书第2版)》_中文完整版

    在这一部分中,还涉及了函数、数组、结构体和指针等C语言元素,因为Objective-C建立在C语言之上,所以对这些基础概念的掌握对于学习Objective-C至关重要。 第二部分关注于Foundation框架的详细阐述。Foundation框架...

    C代码版本的HTTP POST上传文件/JSON

    在C语言中,可以使用开源库如json-c或libjson来解析和生成JSON字符串。在HTTP请求体中,JSON数据通常作为纯文本,Content-Type设为`application/json`。 5. **HTTP状态码**:服务器会返回一个HTTP状态码,以确认...

    json-rpc协议C语言版

    这些库提供了解析JSON对象、数组、字符串、数字等的功能。 2. **RPC框架**:这包括创建请求结构体,定义消息头,处理请求和响应的编码与解码。开发者需要实现一套API,使用户能够轻松地构造JSON-RPC请求和处理响应...

    PHP开发中常用处理函数_50.txt

    #### 一、字符串处理函数 **1. implode()** - **功能**:将数组中的元素合并成一个字符串,中间可以用指定字符分隔。 - **参数**: - `string $glue`:用于连接各元素的粘合符。 - `array $pieces`:需要连接的...

    iPhone开发基础教程&Objective-C.2.0程序设计

    7. **Foundation框架**:Objective-C的基础框架,包含了诸如字符串、数组、字典等基本数据类型,以及线程、URL处理等功能。 8. **Cocoa Touch**:iOS开发中的核心框架,提供了UI组件和触摸事件处理等,如...

    2021-2022计算机二级等级考试试题及答案No.1156.docx

    - 使用字符串比较函数(如C语言中的`strcmp`)可以实现精确的比较。 ### 20. Word 表格 **知识点概述**: - Microsoft Word是一款广泛使用的文字处理软件,其中**表格**是一个重要的功能。 - 在Word中可以创建各种...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    varchar2 1~4000字节 可变长度字符串,与CHAR类型相比,使用VARCHAR2可以节省磁盘空间,但查询效率没有char类型高 数值类型 Number(m,n) m(1~38) n(-84~127) 可以存储正数、负数、零、定点数和精度为38位的浮点数...

    JSON浅谈-2

    JSON是一个完全独立于编程语言的文本格式,但其使用了许多类似C语言家族(包括C, C++, C#, Java, JavaScript, Perl, Python等)的约定。这使得JSON成为了一种理想的数据交换语言。 JSON的基本结构建立在两种构造之...

    一个php我顶程序一个php我顶程序

    PHP支持以下数据类型:整型(int)、浮点型(float)、字符串(string)、布尔型(bool)、数组(array)、对象(object)、NULL以及资源(resource,通常用于外部系统,如数据库连接)。 4. PHP字符串处理: PHP...

    JNI_API.rar_android jni api_jni_jni api_jni api_jni api chm

    - 字符串处理:Java字符串和C字符串之间的转换。 - 数组处理:处理Java数组,包括对象数组和基本类型数组。 - 对象引用:创建和操作Java对象,包括获取字段值和调用方法。 - 类和方法的查找:如何查找Java类及其方法...

    C语言程序设计复习题:.doc

    4. **数组与字符串**:数组是一系列相同类型的元素集合,字符串是字符数组的特例,以空字符'\0'结尾。数组的使用涉及初始化、遍历和传递。 5. **结构体与联合体**:结构体是将不同数据类型的成员组合在一起的数据...

    2021-2022计算机二级等级考试试题及答案No.19611.docx

    - 在Python中,可以使用`len()`函数来获取字符串的长度。 - 类似的,在Java中,字符串对象也有`length()`方法来获取长度。 - 获取字符串长度是编程中常见的需求之一。 ### 7. 视图限制 - **知识点**:在数据库...

    PHP编程起步自学教程

    - **数据类型**:包括整型(int)、浮点型(float)、字符串(string)、数组(array)、对象(object)等。 ##### 2. 控制结构 - **条件语句**:if...else、switch...case用于实现分支逻辑。 - **循环语句**:for、while、...

    全功能PHP代码全功能PHP代码全功能PHP代码

    2. **变量类型**:PHP支持多种数据类型,包括整型(int)、浮点型(float)、字符串(string)、布尔型(bool)、数组(array)和对象(object)。此外,还有NULL类型,表示变量没有值。 3. **字符串操作**:PHP...

    C#详细基础教程(推荐使用)

    - **1.4.10 字符串类(string类)** - 不可变的字符序列。 - **1.4.11 类型转换** - 显式转换(`(type)value`)和隐式转换。 **1.5 运算符** - **1.5.1 运算符分类** - 算术运算符、比较运算符、逻辑运算符等。 - **...

    php7.3.8手册(官网).zip

    `str_replace`, `substr`, `strlen`等函数是处理字符串的关键工具。 5. **面向对象编程** PHP 7.3.8支持面向对象编程,包括类、对象、继承、接口、抽象类、final关键字、魔术方法等。理解这些概念对于构建复杂的...

    电大秋C语言复习题.doc

    字符串操作是编程中的常见需求,C#中字符串的处理可以通过@符号表示verbatim字符串,以及使用转义序列(如\t表示制表符,\n表示换行)来简化字符串的书写和表达。 异常处理是C#语言中保证程序健壮性的重要机制。try...

    2021-2022计算机二级等级考试试题及答案No.10339.docx

    - **详细说明**:题目提供了一段Java代码,其中使用`Double.parseDouble()`和`Integer.parseInt()`方法将字符串转换为双精度浮点数和整数。这里的关键在于理解字符串到数值类型的转换过程及其操作顺序。最终输出的...

Global site tag (gtag.js) - Google Analytics