`
linwwwei
  • 浏览: 226595 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Object-c 中字符串总结

 
阅读更多
//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,这一点前面在串中搜索子串用到过;
分享到:
评论

相关推荐

    Object-c字符串数组字典

    在Objective-C语言中,字符串、数组、字典以及集合是常用的数据结构类型。这些类型分为可变和不可变两种,它们分别对应不同场景下的需求。在Objective-C中,字符串被封装在NSString类中,数组由NSArray和...

    Object-C字符串

    3. **从C字符串创建Objective-C字符串**:有时候需要将C语言中的字符串转换为Objective-C中的`NSString`对象。 ```objc NSString *fromCString = [NSString stringWithCString:"A C string" encoding:NSUTF8String...

    Object-C语言教程.docx

    - 字符串在Object-C中以`@`开头,如`@"hello"`。 #### 四、面向对象编程 - **核心概念**: - **类与对象**:类是对对象的抽象描述;对象是类的具体实例。 - **继承**:允许一个类继承另一个类的属性和方法。 -...

    数据库字符串相加---字符串相加函数

    在数据库操作中,经常需要将多个字符串连接成一个新的字符串。例如,在报表生成、日志记录、数据导出等场景下,将多条记录的数据合并成一段文本是非常常见的需求。SQL Server 2000内置了一些字符串处理函数,但它们...

    Object-C语言教程&

    这个简单的示例展示了如何在Objective-C中使用NSLog函数输出字符串“Hello World!”到控制台。需要注意的是,`@autoreleasepool`是Objective-C中用于管理内存的自动释放池,确保内存被适时释放。 #### 七、总结 ...

    Object-c基础教程例程

    Objective-C(Object-c)是Apple公司为Mac OS X和iOS操作系统开发的一种面向对象的编程语言,它是C语言的超集,融入了Smalltalk的关键特性。本教程旨在为初学者提供一个全面的Objective-C入门指导,涵盖了从基本语法...

    详细描述了Object-C中的内存管理,超级简单

    为了实现这个功能,我们需要创建一个数组来存储各种寿司类型的名称,并在每次显示一行时从数组中取出相应的字符串。 1. **创建数组**: - 首先,在`RootViewController`类中声明一个名为`_sushiTypes`的实例变量,...

    Object-C语言教程&案例&相关项目资源

    - **描述**:此方法返回对象的字符串表示形式,默认情况下,返回类名和对象的十六进制地址。 - **应用场景**: - 通常用于调试和日志记录。 - 当需要以更友好的方式表示对象时,可以重写此方法以提供更多的信息。 ...

    详解protobuf-c之在C语言中如何使用repeated生成数组和字符串(包含配置pb-callback-t)

    本篇文章将详细解释如何在C语言环境中使用protobuf-c处理`repeated`字段,创建数组和字符串,并特别关注`pb_callback_t`这一特殊类型。 首先,我们需要理解`repeated`字段在protobuf语义中的含义。在protobuf的定义...

    Object-c学习文档

    ### Object-C 学习文档知识点总结 #### 开始学习Objective-C **下载教程:** - 所有的初学者指南原始代码可从`objc.tar.gz`下载。 - 许多示例来自Steve Kochan的《Objective-C 编程》一书。若需更深入的学习和示例...

    C#字符串总结

    在C#编程中,字符串操作是非常常见的任务,无论是数据处理还是用户交互,都需要对字符串进行各种操作。C#提供了一种内置的`string`类型,它实际上是对.NET Framework中的`System.String`类的一个别名。这里我们将...

    Object-C_Runtime

    在Objective-C内部,`SEL`实际上是一个C字符串(即`char *`),但在外部则被抽象为`struct objc_selector *`。`@selector`则是Objective-C提供的一种语法糖,用于创建`SEL`类型的值。 - **为何不直接使用C字符串?*...

    Programming in object-c 2.0.pdf

    // 字符串对象 ``` ##### 2.3 面向对象编程(OOP) - **类(Class)**:类是面向对象编程的基本单位,它是对象的模板或蓝图。 - **对象(Object)**:对象是类的一个实例。 - **方法(Method)**:方法是类中的函数,...

    C_中字符串操作总结.pdf

    总结来说,C#字符串操作涉及到了字符串的基本知识、不可变性、字符串比较的不同方法、使用StringBuilder进行动态字符串操作以及处理空字符串和null字符串的区别。掌握这些知识点对于进行高效、正确的字符串操作至关...

    python中字符串比较使用is、==和cmp()总结

    在 Python 中比较字符串最好是使用简单逻辑操作符。 例如,确定一个字符串是否和另外一个字符串匹配。正确的,你可以使用 is equal 或 == 操作符。你也可以使用例如 >= 或 < 来确定几个字符串的排列顺序。 从...

    cJSON库(json格式字符串进行转化)

    在这个例子中,我们首先解析了一个JSON字符串,然后获取了其中的"name"和"age"字段。接着,我们创建了一个新的JSON对象,并添加了"name"和"age"字段,最后将其转换回JSON字符串并打印。 **6. 注意事项** 使用cJSON...

    js中的json对象和字符串之间的转化

    虽然描述为空,但我们可以推断,这个博客可能详细介绍了如何在JavaScript中进行JSON对象与字符串的转化操作,以及在实际应用中的重要性。 **标签中的知识点:** 1. **源码**:这可能意味着博客可能包含了实际的...

    Object-C加减乘除的案例

    - **结果输出**:使用`printf`函数输出计算结果,格式化字符串`%.2lf`控制结果保留两位小数。 #### 总结 通过本程序的学习,我们了解了Objective-C中如何实现基本的算术运算功能,以及如何通过控制流语句(如`...

    JAVA字符串处理函数列表一览

    - `boolean equals(Object anObject)`:如果当前字符串与参数`anObject`表示的字符串相等,则返回`true`。 - `boolean equalsIgnoreCase(String anotherString)`:如果忽略大小写后的当前字符串与`anotherString`...

Global site tag (gtag.js) - Google Analytics