IOS基础:Objective-C 字符串处理

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

/*----------------从文件读取字符串: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,这一点前面在串中搜索子串用到过;

时间: 2024-08-26 16:01:17

IOS基础:Objective-C 字符串处理的相关文章

iOS基础问答面试

<简书社区 — Timhbw>iOS基础问答面试题连载(一)-附答案:http://www.jianshu.com/p/1ebf7333808d <简书社区 — Timhbw>iOS基础问答面试题连载(二)-附答案:http://www.jianshu.com/p/ce50261f8907 <简书社区 — Timhbw>iOS基础问答面试题连载(三)-附答案:http://www.jianshu.com/p/5fd65c20912e 以下是一些自己收集的比较基础的问题(

iOS基础 KVC和KVO

疯狂IOS讲义这本书之前一直一直觉得没什么用,看了做不出像样的程序出来,但是经过几天的学习发现,没有一定的ios基础,做的程序永远都是在模仿他人的程序,把他人的代码复制 粘贴...为什么能够实现?原理 是什么?不懂...所以 必须懂原理,为什么? 接下来就今天所学习的内容做一个小结: 首先介绍一下KVO和KVC,一个是键值编码一个是键值监听,我第一次听这个也是很困惑,这是个神马意思?键值是啥?监听 还懂一点 ... 我们都知道之前我们都是通过setter和getter来设置和修改对象的属性,KV

iOS 基础 第二天(0805)

0805 面向对象三大特性 封装.继承和多态 oc的方法都是在运行过程中才会检测的.编译时方法没实现只会出现警告,运行时出错.如果方法实现了但没有声明,运行时对象仍然可以调用方法不会出错.这是OC中弱语法的表现 说白了oc中的弱语法就是因为运行时检测合理性和可用性.编译时不会出错顶多是警告,运行时才警告.这个现象不仅仅体验在方法的声明和实现上,比较好的一个例子是MPMoviePlayerController的截屏通知事件,它需要传入float类型的数组,如果你在编译写了整型不会报错也不会警告,但

iOS基础问答面试题连载(三)-附答案

「Tim的博客」iOS基础问答面试题连载(一)-附答案 「Tim的博客」iOS基础问答面试题连载(二)-附答案 「Tim的博客」iOS基础问答面试题连载(三)-附答案 「Tim的博客」iOS基础问答面试题连载(四) 这次的问题是网络多线程相关的哟,面试的时候也是必问的,大家多看看 11月24日修正一处错误:18.19题目一样,答案不一样(其实是两种理解,修改为最优的一种放上来.多谢读者提醒) 以下是一些自己收集的网络多线程方面比较基础的问题(大神可以忽略),附上答案,方便大家阅读.俗话说得好,基

ios基础-XCode使用技巧

(一)代码规范pragma mark 1.定义 #pragma 开头的代码是一条编译器指令,是一个特定于程序或编译器的指令.不一定适用于其它编译器或其它环境.如果编译器不能识别该指令,则会将其忽略. 2.作用 在编辑器窗格顶部,方法和函数弹出菜单中将代码分隔开,规范化代码,方便阅读查找. 3.使用 在需要加注释的地方加上#pragma mark - #pragma mark - 视图将要显示的时候 - (void)viewWillAppear:(BOOL)animated { //初始化选号的数

iOS 基础函数解析 - Foundation Functions Reference

Foundation Functions Reference Framework Foundation/Foundation.h Declared in NSBundle.h NSByteOrder.h NSDecimal.h NSException.h NSObjCRuntime.h NSObject.h NSPathUtilities.h NSRange.h NSZone.h Overview This chapter describes the functions and function

【C++基础】 指针&amp;字符串&amp;数组

先贴代码,总结以后再写,和5中内存分配方式密切相关 PS: str 返回整个字符串,str[0],*str返回首字符h char *strA(){ char str[]="hello!"; //局部数组,局部变量, str存在栈区 return str; //局部变量不能传值,估计会成为野指针 //函数返回局部变量的地址,当被调用完成时,str就释放了,因此返回结果是不确定的且不安全的 } char *strA2(){ char *str = "hello2!";/

面向对象基础(class0425)字符串与集合

常用类库 学习.net就是学习它的无数个类库怎么用,先看两个简单的 String 字符串,不可变特性.字符串可以看成字符数组 属性 Length 方法 IsNullOrEmpty() 静态方法 ToCharArray() ToLower() string不可变性 ToUpper() Equals() 忽略大小写的比较 Join() 静态方法 Format () 静态方法 IndexOf() LastIndexOf() Substring() Split() Replace() Trim()  C

iOS 基础之—— Properties

一篇关于iOS 编程中 @property 方法中属性的介绍,写的十分简洁明了,原文链接请戳. ===读后分割线==== 在iOS 5之后,有了ARC 的出现,不需要手动进行内存管理,@property 中的属性也有了一些变化. 现在常使用的如下: 属性 功能介绍 getter= 给getter方法一个自定义名称 setter= 给setter方法一个自定义名称 readonly 不生成setter方法 nonatomic 禁止多线程,相较默认的线程保护(atomic)可提高性能 内存管理属性