`
反求诸己
  • 浏览: 544902 次
  • 性别: Icon_minigender_1
  • 来自: 湖南娄底
社区版块
存档分类
最新评论

iphone开发NSString字符串常用方法

 
阅读更多

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





    


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

//}


分享到:
评论

相关推荐

    iPhone之NSString常用方法示例程序

    在开发iPhone应用时,字符串操作是非常常见的,NSString提供了丰富的API来处理这些需求。 首先,NSString是不可变的,这意味着一旦创建了NSString对象,就不能更改它的内容。如果你需要修改字符串,通常需要创建一...

    iphone 开发学习基础NSString

    字符串的合并是另一个常用操作,可以使用`+`运算符或`stringByAppendingString:`方法将两个字符串连接在一起。例如:`NSString *str1 = @"Hello"; NSString *str2 = @"World"; NSString *combined = [str1 ...

    iphone 开发比用的NSString转化技巧

    这些转换技巧在日常的iPhone应用开发中非常常见,熟练掌握这些方法能帮助开发者更高效地处理字符串和其他类型的数据。在实际项目中,还需要根据具体需求和场景灵活运用,确保代码的健壮性和性能。

    iOS NSString详细操作

    作为`NSObject`的子类,它不仅继承了父类的属性和方法,还提供了丰富的字符串处理功能。对于iOS开发者来说,熟练掌握`NSString`的使用方法是非常重要的。 #### 二、创建`NSString`对象 1. **直接初始化**: - **...

    iphone 开发学习基础char

    然而,在Objective-C中,我们更倾向于使用`NSString`来处理字符串,因为它提供了丰富的字符串操作方法。将`char *`转换为`NSString`,可以使用`[NSString stringWithUTF8String:]`方法: ```objc const char *...

    iPhone开发常用类型的参考

    2. **NSString_Class.pdf**:NSString是Objective-C中的基础类,用于处理文本字符串。文件可能涵盖了创建和操作字符串的方法,如拼接、分割、查找子字符串、格式化输出等,同时也会涉及到字符串的比较、大小写转换和...

    Objective-c中的字符串格式化输出

    在Objective-C中,字符串格式化输出是编程时常见的任务,特别是在iPhone开发中,开发者经常需要在控制台上打印信息以调试程序。Objective-C提供了类似于C语言的printf函数的字符串格式化方法,但它是通过`NSString`...

    iphone开发常用代码

    ### iPhone开发常用代码知识点 #### 一、URL编码方法 在进行网络请求时,经常会遇到需要对字符串进行URL编码的情况,以确保传输过程中不会出现问题。下面的代码展示了一个简单的URL编码函数实现: ```objective-c...

    iphone开发快速入门.pdf

    总结来说,Objective-C是iOS开发的关键语言,它的面向对象特性、字符串处理、类和方法的定义以及消息传递机制为开发者提供了强大的工具。通过深入学习这些基础知识,开发者能够迅速进入iPhone应用开发领域。

    Iphone_开发常用代码

    2. **匹配字符串:** 使用`firstMatchInString:options:range:`方法来检查指定的字符串是否符合正则表达式。 #### 四、加载图片 在iOS应用中加载图片是常见的需求,可以通过多种方式实现。 **从本地加载图片:** `...

    iphone生命周期 详解

    在深入探讨iPhone的生命周期之前,我们先简要回顾一下Objective-C中字符串处理的相关知识,这部分内容虽然不直接涉及iPhone的生命周期,但能够帮助更好地理解iOS开发的基础知识,进而为深入理解iPhone的生命周期奠定...

    iphone存储用法汇总

    从给定的文件标题“iPhone存储用法汇总”和描述“PDF中包含有iOS数据的存储类型详细介绍”,我们可以深入探讨iOS开发中常见的数据存储技术,特别是对于基础数据类型的使用,如字符串、数组和字典。以下是对这些核心...

    iphone开发学习笔记(1)--NSLog

    在这里,`@`符号表示一个Objective-C的字符串字面量,它会被直接转换为`NSString`对象。`格式化字符串`可以包含占位符,如`%@`(用于`NSObject`子类),`%d`(用于`int`),`%s`(用于C风格的字符串),`%f`(用于`...

    iphone开发快速入门[收集].pdf

    NSString 支持可变长度的字符串和Unicode,提供了许多实用的方法,如格式化字符串、比较字符串等。创建NSString对象时,可以使用@符号直接定义字符串常量,或者通过`stringWithFormat:`方法动态生成字符串。 类...

    新手入门常用的iPhone代码

    本文将围绕一个标题为“新手入门常用的iPhone代码”的主题进行展开,详细介绍其中提及的一些基本操作,包括字符串处理、日期与时间的管理、时区处理以及日历相关的操作等。这些知识点都是iOS开发中非常实用且经常...

    iPhone开发基础 cocoa入门

    - **字符串**:Foundation框架提供了NSString类,支持复杂的文本处理。 - **集合(Collection)**:NSArray、NSSet和NSMutableDictionary等类提供了对列表、集合和字典的支持。 - **内存管理**:Objective-C中使用...

    iphone开发面试题(2)套

    **总结:** 这道题展示了如何使用 NSString 类提供的初始化方法来创建字符串对象, 对于处理不同编码的文本数据非常重要。 以上题目涵盖了 iOS 开发中的一些基础知识和技术要点, 包括指针与数组、`sizeof` 运算符、...

    iPhone开发资料之Objective-C语言简介

    `NSString`是一个不可变的字符串对象,支持Unicode,方便进行格式化和内存管理。创建字符串可以通过直接赋值或使用`stringWithFormat:`方法: ```objc NSString *myString = @"My String"; NSString *anotherString...

    iphone开发json解析详细讲解

    - JSON要求所有的键都是字符串,所以在Objective-C中,字典的键必须是NSString类型。 - 当解析JSON时,如果数据不合法,`JSONObjectWithData:options:error:`会返回nil,并通过error参数提供错误信息。 - 解析...

    iphone 开发学习项目创建及调试输出NSLog

    NSString *test = @"直接打印字符串"; NSLog(@"打印外部对象:%@", test); NSLog(@"打印整数i=%d", 5); int i = 5; NSLog(@"int i=%d", i); char *c = "123456"; NSLog(@"char c=%s", c); const char *cc = [@...

Global site tag (gtag.js) - Google Analytics