//1、创建常量字符串。
NSString *astring = @"This is a String!";
//2、创建空字符串,给予赋值。
NSString *astring = [[NSString alloc] init];
astring = @"This is a String!";
NSLog(@"astring:%@",astring);
[astring release];
//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为真)
//如何判断字符串为空
NSString *urlString = [urlInput stringValue];
if (!urlString) {
NSLog( @”NO INPUT.” );
} else {
if ([urlString length] == 0 ) {
NSLog( @”NO INPUT.” );
} else {
}
}
}
Convert NSString to int
1 NSString *aNumberString = @"123";
2 int i = [aNumberString intValue];
Convert int to NSString
1 int aNumber = 123;
2 NSString *aString = [NSString stringWithFormat:@"%d", aNumber];
分享到:
相关推荐
ytoolkit 是 Objective-C 的一个常用开发工具包,包括: 一个快速的base64编码(纯C,及NSData/NSString的category),可以看项目页面的README和代码库里的benchmark,或者在ytoolkitdemo里跑profile一个cocoa附加...
主要介绍了Objective-C中字符串NSString的常用操作方法总结,Objective-C中NSString和NSMutableString这两个类下包含了操作字符串的大多数方法,需要的朋友可以参考下
前几天提前看到了新出的<Objective-C 2.0 程序设计>, 也就是<Programming in Objective-C 2.0(Section Edition)>的中文翻译版, 翻看了几章, 总体上感觉还是不错的. 翻译的质量我觉得比较满意吧. 个人觉得, 对一个还...
基础语法:熟悉C语言基础是前提,Objective-C是在C语言的基础上增加了面向对象特性。 面向对象:理解类(Class)、对象(Object)、实例变量(Instance Variables)、方法(Methods)、继承(Inheritance)、封装...
Objective_C Foundtion Kit的核心类 常用的数据结构NSRange、NSPoint、NSSize、NSRect、NSString、NSArray、NSEnum、NSDictionary、NSNull等。 有Demo.
Objective-C runtime是一个实现Objective-C语言的C库。它是一门编译型语言、也是一门动态型的语言(这里强调下OC是静态类型语言),之前没接触runtime的时候也不觉着它有多重要,接触之后才发现其实runtime挺强大的...
objective-c foundation框架,NSString,NSArray,等类的常用方法ppt
Objective-C + (void)alertWithMessage:(NSString *)message; + (void)alertWithTitle:(NSString *)title message:(NSString *)message; + (void)alertWithTitle:(NSString *)title message:(NSString *)message ...
XBUserDefaultsXBUserDefaults是利用Objective-C RunTime 机制对NSUserDefaults进行一层封装。大大增加了易用性和可维护性。性能基本毫无影响(100000次NSString读写,耗时仅多0.4s±0.05s)。几乎每个iOS项目中都会...
XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具,大大简化了基于XMPP的通信应用的开发。 1. 登录和好友上下线 1.1XMPP中常用对象们 ...
源码包含了近1000个Emoji表情,全部最常用的Emoji表情以及一大部分冷门表情. ##使用说明: iOS支持表情的直接显示,但是为了兼容web以及Android等,上传到服务器的数据以及请求下来的数据都要做相应的数据转换 表情...
(void)showSuccess:(NSString *)success toView:(UIView *)view; /** 显示失败信息提示框 @param success 失败信息 @param view 指定显示信息的view */ (void)showError:(NSString *)error toView:(UIView *)view; /...
笔者对部分常用设置进行封装,如:设置字体大小、颜色、样式等。介绍资源文件说明ZSSRichTextEditor.js文本及图片的设置editor.html模板封装文件说明ERichTextEditorView对JS方法进行封装ContentEditFooterView底部...
ExtKit实现了一些平时常用的功能,提供开发效率 platform :ios, '6.0' pod 'ExtKit', '~> 0.2' ##简单介绍 ####NSData/NSString 1.AES/DES/3DES 加解密 2.MD5,SHA1,SHA224,SHA256,SHA384,SHA512 签名验证 3.Base64 ...
DYModelMaker中文说明安装使用cocoapods pod'DYModelMaker'当前最新版本1.0.1,如果不能搜索最新版本,执行命令pod setup更新pod后再试。持续优化中,如果有好多建议和意见欢迎交流,QQ群:...生成常用第三方MJExtens
RZUtils 适用于iOS开发的有用实用程序和组件的集合。安装可可豆按目录将RZUtils的podspec完全... 例如: pod 'RZUtils/Categories/NSString' pod 'RZUtils/Categories/KVO' pod 'RZUtils/Components/RZProgressView'
ZPCusCount//textfeild 是否允许手动输入(默认NO)@property (nonatomic,assign) BOOL is...//设置textfeild的值@property (nonatomic,copy) NSString *textCount;//设置textfeild的最大值@property (nonatomic,co
它目前包含以下模块: 加密:HMAC 实用程序UI:UIView、UIViewController、UINavigationController 等方便的分类UICollectionView:UICollectionView 的常用布局Util:通用工具(例如NSString、NSObject)安装包含...
FCQRCodeScanner 简单的QR码扫描仪和生成器(Apple API)支持平台iOS 7.0的常用功能。 iOS 8.0用于本地图片解码。 [和设备限制:以上设备的iPhone 5S支持此功能(包括5S)]安装将FCQRCodeScanner文件夹拖到您的项目...