Objective-C语法基础之NSString

//在字符串中,动态插入字符串

//由于返回来的链接"www.yousawang.com"中少了"/",故在"yousawang.com"后面加上"/"

NSMutableString *string_logo=[NSMutableStringstringWithString:user.Store_logo];

NSRange range = [string_logorangeOfString:@"yousawang.com"];

[string_logoinsertString:@"/"atIndex:range.location+13];

NSLog(@"str=%@",string_logo);

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

[email protected]"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);

//}

时间: 2024-08-13 10:44:10

Objective-C语法基础之NSString的相关文章

Objective C Foundation基础框架[转]

<pre class="cpp" name="code">iOS Foundation基础框架就是以Foundation.h头文件的库 #import <Foundation/Foundation.h> Mac OS X和iOS都使用了Foundation基础框架.该框架中有很多日后开发常用的API,非常基础重要.以下就常用的类和结构做个介绍. NSObjct NSObject类,是ObjC类族中的根类.NSObject有一些高级特性支持,在

Java语法基础

Java语法基础 1.  关键字 某些单词对编译器有着特殊的含义,并且不能作为标示符使用,全部是小写字母 Java语言关键字 abstract boolean break byte case catch char class try do default continue double else extends assert final finally float for If implement import instanceof int interface long native new g

java语法基础一

Java语法基础一 Java代码基本格式 Java中所有程序代码都必须存在于一个类中,用class关键字定义类,在class之前可以有一些修饰符.格式如下: 修饰符 class 类名 { 程序代码 } 注:1.Java是严格区分大小写的. 2.Java程序中一句连续的字符串不能分开在两行中写. Java程序的注释 Java里的注释有三种类型: 1.单行注释 在注释内容前面加“//”,格式为: 代码; //注释内容 2.多行注释 以斜杠加星号开头,以星号加斜杠结尾. 3.文档注释 以斜杠加两个星号

Swift语法基础入门三(函数, 闭包)

Swift语法基础入门三(函数, 闭包) 函数: 函数是用来完成特定任务的独立的代码块.你给一个函数起一个合适的名字,用来标识函数做什么,并且当函数需要执行的时候,这个名字会被用于“调用”函数 格式: func 函数名称(参数名:参数类型, 参数名:参数类型...) -> 函数返回值 { 函数实现部分 } 没有参数没有返回值 可以写为 ->Void 可以写为 ->() 可以省略 Void.它其实是一个空的元组(tuple),没有任何元素,可以写成() func say() -> V

javascript语法基础-变量与函数

三 javascript语法基础-变量与函数 (一)变量的声明与运用 JavaScript中的变量与Java.C等强类型语言有很大区别,虽然在JavaScript中具有字符串.数字等数据类型. 变量申明语句的结构是var保留字加标识符,var和标识符之间用空格隔开. 赋值语句的结构是在变量和需要赋的值之间加上一个等号,例如a=1的含义是将变量a的值指定为1. 变量在定义的时候也可以同时赋值,如var a=1. PS:在变量使用前事先进行声明是个良好的编程习惯,这对将来学习Java等其他语言有帮助

php语法基础

php变量 php变量用于存储字符,数字,数组甚至对象资源等,以便在我们需要的地方使用. $变量名=值; 变量名以字母(a-z,A-Z)或者下划线_开始,后面可以跟 任意字母或数字以及下划线,但不能是空格. 例子: <?php $var_char="你好"; echo $var_char; ?> 结果为:你好! 延伸:与c语言等强类型的编程语言不通,php 是一门松散类型的语言,即不需要在设置变量之前 声明该变量.根据变量被设置的方式,php会自动 地将变量转换成正确的数据

C#-01.语法基础

a. 语法基础 i. 命名空间(namespace):是 C# 中组织代码的方式,用来声明命名空间 1. 语法:namespace 命名空间名称{ //命名空间的声明 } 2. 作用:可以把紧密相关的一些代码放在同一个命名空间中,大大提高管理和使用的效率 3. 与 Java 的不同处:Java 的是使用 package(包) 的关键字,作用是与 namespace 类似 i. using 关键字:用来引用其他命名空间 1. 语法:using 类名; 2. 与 Java 的不同处:Java 的是使

iOS复习笔记2:Objective-C语法基础

一 语法基础 1 关键字 关键字基本上都是以@开头,常见关键字如下: @interface,@implement,@end,@public,@private,@selector,@required,@encode等 其他id,self,super等 2 字符串以@开头 @"Hello world!" 3 布尔类型Yes/No 4 空类型nil(值为0) 5 其他C语言语法 二 OC的HelloWorld程序 // helloworld.m #import <Foundation/

java基础知识(2)---语法基础

二:java语法基础: 1,关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:其实就是还没有赋予特殊含义,但是准备日后要使用过的单词. 2,标示符:其实就是在程序中自定义的名词.比如类名,变量名,函数名.包含 0-9.a-z.$._ : 注意: 1),数字不可以开头. 2),不可以使用关键字. 3,常量:是在程序中的不会变化的数据. 4,变量:其实就是内存中的一个存储空间,用于存储常量数据. 作用:方便于运算.因为有些数据不确定.所以确定该数据的名词和存储空间. 特点:变量空间可以重复使用