NSString的常见方法
NSString *astring = @"This is a String!";
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);
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); [astring release];
char *Cstring = "This is a String!"; NSString *astring = [[NSString alloc] initWithCString:Cstring]; NSLog(@"astring:%@",astring); [astring release];
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];
NSString *astring; astring = [NSString stringWithCString:"This is a temporary string"]; NSLog(@"astring:%@",astring);
//7、从文件创建字符串
NSString *path = [[NSBundlemainBundle] pathForResource:@"astring.text"ofType:nil];
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
//8、用字符串创建字符串,并写入到文件
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES]; [astring release];
注:此路径path只只是示意,真实路径并非如此
char string1[] = "string!"; char string2[] = "string!"; if(strcmp(string1, string2) == 0)
{ NSLog(@"1"); }
//10、isEqualToString方法
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result);
//11、compare方法(comparer返回的三种值)
//
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedSame; //NSOrderedSame判断两者内容是否相同 NSLog(@"result:%d",result); //
NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedAscending; //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) NSLog(@"result:%d",result); //
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] == NSOrderedDescending; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) NSLog(@"result:%d",result);
//12、不考虑大小写比较字符串
//1.
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) NSLog(@"result:%d",result); //2.
NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame; //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 NSLog(@"result:%d",result);
NSString *string1 = @"A String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
//14、-rangeOfString: //查找字符串某处是否包含其它字符串
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];
//15、-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringToIndex:3]; NSLog(@"string2:%@",string2);
//16、-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringFromIndex:3]; NSLog(@"string2:%@",string2);
//17、-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string2:%@",string2);
//18、-stringWithCapacity: //按照固定长度生成空字符串
NSMutableString *String; String = [NSMutableString stringWithCapacity:40];
//19、-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);
//20、-insertString: atIndex: //在指定位置插入字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 insertString:@"Hi! " atIndex:0]; NSLog(@"String1:%@",String1);
//21、-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 setString:@"Hello Word!"]; NSLog(@"String1:%@",String1);
//22、-replaceCharactersInRange: withString: //用指定字符串替换字符串中某指定位置、长度的字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; NSLog(@"String1:%@",String1);
//23、-hasPrefix: //检查字符串是否以另一个字符串开头
NSString *String1 = @"NSStringInformation.txt"; [String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); [String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
//25、文件扩展名
NSString *Path = @"~/NSData.txt"; NSLog(@"Extension:%@",[Path pathExtension]);
NSString的常见方法的更多相关文章
- NSString 的常见方法
NSString的常用方法 创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSUR ...
- iOS-NSString常见方法
</pre><pre name="code" class="html">#import <Foundation/Foundatio ...
- UIPickerView常见属性、常见方法(包括代理方法和数据源方法)的一些说明
一.UIPickerView 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id< ...
- C#图片处理常见方法性能比较
C#图片处理常见方法性能比较 来自:http://www.cnblogs.com/sndnnlfhvk/archive/2012/02/27/2370643.html 在.NET编程中,由于GDI ...
- window对象中的常见方法
<body><!-- window对象中的常见方法--><script type="text/javascript"> var timeid; ...
- python socket 常见方法及 简单服务/客户端
socket 常见方法: 补充说明:what is file descriptor? 文件描述符是什么? 参考(http://stackoverflow.com/questions/8191905/w ...
- VBS操作Excel常见方法
VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- AJAX跨域的常见方法
由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问.比如说你的网站域名是aaa.com,想要通过AJAX请求 ...
随机推荐
- CDH中flume是已经启动着了…
文章来自:http://www.cnblogs.com/hark0623/p/4174646.html 转发请注明 在CDH中用了几天flume后才发现,原来CDH中的flume默认是启动的……… ...
- node相关--测试
测试: assert模块; //node自带 var assert = require('assert'); var now = Date.now(); console.log(now); asser ...
- Xamarin Anroid开发教程之Anroid开发工具及应用介绍
Xamarin Anroid开发教程之Anroid开发工具及应用介绍 Xamarin开发Anroid应用介绍 如今智能手机已经盛行了好几年,而针对这些智能手机的软件开发也变得异常火热.但是在Andro ...
- http://www.cnblogs.com/yjmyzz/p/3941043.html
http://www.cnblogs.com/yjmyzz/p/3941043.html
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ1699 Best Sequence(AC自动机+状压DP)
题目,求包含所有的给定的n个DNA片段的序列的最短长度. AC自动机上的DP题. dp[S][u]表示已经包含的DNA片段集合为S,且当前后缀状态是自动机第u个结点的最短长度 dp[0][0]=0 我 ...
- java中的Integer的toBinaryString()方法
在一次面试的过程中,遇到过这样的题目,题目的大概意思是:让写出Integer类中的toBinaryString()方法 也就是说,把Integer转换为Binary的过程写出来 但是我蒙的,在查了JD ...
- jquery and jquery validation 常见问题解决
Cannot read property 'settings' of undefined jquery validation 这个问题常常发生在动态添加rule的时候. 解决方法 // 在timeou ...
- PHPUnit在Windows下的配置及使用
由于我们项目涉及到php,因此需要对php代码进行单元测试.经过一番了解,决定用PHPUnit来测试php.PHPUnit花了不少时间摸索如何配置PHPUnit,看官网的文档也是一把泪.但知道怎么配置 ...
- SQL Server 中的触发器(trigger)
SQL Server 触发器 触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 ...