惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - 黎波

精至手机药典Windows Phone 7版 [WP7]修改 Pivot 控件的 PivotItem 标题字体大小 [WP7]CheckBox 文字自动换行 Windows Phone 7 系统主题颜色RGB和Hex值 [WP7]MD5加密字符串 [iOS]MD5加密字符串 [WP7]关于 ListBox 的 SelectionChanged 事件 [Android]MD5加密字符串 [Android]获取设备IP地址 如何检查对象的类型[iOS/Android/Windows Phone] 精至手机药典iPhone版 目标身高iPhone版 目标身高Android版 [iOS]让Xcode 4.2生成的app支持老的iOS设备(armv6) [iOS]升级到 Xcode 4.1 后 sqlite3.h 引起 Expected declaration specifiers before 'interface' 错误 [转载]用手机玩转汽车 体验安吉星手机应用程序 [iOS]Xcode 4.1 bug: Text Field 引起 EXC_BAD_ACCESS 错误的解决 精至手机药典Android版 [Android]使用ProGuard遇到“conversion to Dalvik format failed with error 1”错误的解决办法
[iOS]NSString常用代码
黎波 · 2011-12-07 · via 博客园 - 黎波

1.判断字符串是否为空

if ([text lenght] == 0) {
    // empty string
}

2.字符串连接

NSString *str1 = @"str1";
NSString *str2 = @"str2";
NSString *result;

//方法1
result = [str1 stringByAppendingString:str2];
NSLog(result, nil);

//方法2
result = [NSString stringWithFormat:@"%@%@", str1, str2];
NSLog(result, nil);

//方法3
result = [@"" stringByAppendingFormat:@"%@%@", str1, str2];
NSLog(result, nil);

//方法4
NSMutableString *ms = [[NSMutableString alloc] init];
[ms appendString:str1];
[ms appendString:str2];
NSLog(ms, nil);
[ms release];
    
//结果都是:str1str2

 一般推荐使用方法1,如果需要大量字符串连接推荐使用方法4,需要更少的内存开销。

3.去除字符串首尾的空格和换行符

NSString *text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

4.多行书写字符串常量

NSString *str1 = @"SELECT [CustomerID], [CustomerName] "
"FROM [Customer] "
"WHERE [CustomerID] = 1234";

NSString *str2 = @"SELECT [CustomerID], [CustomerName] \
FROM [Customer] \
WHERE [CustomerID] = 1234
";

NSLog(str1, nil);
NSLog(str2, nil);

//结果都是:SELECT [CustomerID], [CustomerName] FROM [Customer] WHERE [CustomerID] = 1234

注意字符串中每行结尾处的空格。这种字符串声明方式虽然看上去是多行,实际上字符串中并没有换行符,也就是说整个字符串实际上是一行。如果需要在字符串中换行,可以在字符串中加入换行符"\n"。这种声明方式一般用在需要在代码中多行显示字符串以便提高可读性,例如:SQL语句往往需要多行显示来提高可读性、较长的文本的段落之间需要分行显示以便更容易找到分段位置。

此文将不断更新...

作者:黎波
博客:http://bobli.cnblogs.com/
日期:2011年12月7日