[cpp]  view plain copy

 
  1. //NSString 操作均不改变自身值
  2. //构建字符串
  3. NSString *szTmp = @"A string";      //直接赋值
  4. szTmp = nil;
  5. int n = 5;
  6. NSString *szMyString = [NSString stringWithFormat:@"The number is %d",n];   //The number is 5
  7. [szMyString stringByAppendingFormat:@"%d",22];  //附加字符串返回值:The number is 522
  8. //但是szMyString本身并没有改变,其值依然:The number is 5
[cpp]  view plain copy

 
  1. //长度与索引字符
  2. NSLog(@"%d",szMyString.length);                 //字符串长度:15
  3. NSLog(@"%c",[szMyString characterAtIndex:2]);   //返回字符:e
[cpp]  view plain copy

 
  1. //与c字符串相互转换
  2. printf("%s\n",[szMyString UTF8String]);         //转为__strong const char *
  3. const char *szTmp1 = [szMyString cStringUsingEncoding:NSUTF8StringEncoding];
  4. printf("%s\n",szTmp1);                          //转为__strong const char *
  5. NSLog(@"%@",[NSString stringWithCString:szTmp1 encoding:NSUTF8StringEncoding]); //转为nsstring
[cpp]  view plain copy

 
  1. //字符串写文件
  2. NSError *error;
  3. NSString *szPath = [NSHomeDirectory()           //应用程序沙盒路径
  4. stringByAppendingPathComponent:@"Documents/testFile.txt"];  //附加路径地址
  5. if (![szMyString writeToFile:szPath atomically:YES  //atomically:是否是原子访问文件的
  6. encoding:NSUTF8StringEncoding error:&error]) {          //写入成功返回yes 否则no
  7. NSLog(@"Error writing to file :%@",[error localizedDescription]);       //输出错误描述
  8. return 1;
  9. }
  10. NSLog(@"File write success");
[cpp]  view plain copy

 
  1. //文件读字符串
  2. NSString *szInString = [NSString stringWithContentsOfFile:szPath            //读取文件信息
  3. encoding:NSUTF8StringEncoding error:&error];
  4. if (!szInString)
  5. {
  6. //失败
  7. }
  8. NSLog(@"%@",szInString);        //成功
[cpp]  view plain copy

 
  1. //字符串转为数组
  2. NSArray *arrayWord = [szMyString componentsSeparatedByString:@" "]; //有空格的拆分为单词保存
  3. NSLog(@"%@",arrayWord);
[cpp]  view plain copy

 
  1. //索引子串
  2. NSString *szSub1 = [szMyString substringToIndex:3];     //0-2,前3个:The
  3. NSLog(@"%@",szSub1);
  4. NSString *szSub2 = [szMyString substringFromIndex:4];   //4-尾,去掉前4个:number is 5
  5. NSLog(@"%@",szSub2);
[cpp]  view plain copy

 
  1. //范围索引
  2. NSRange range;
  3. range.location = 4;     //从4开始
  4. range.length = 6;       //6个字符
  5. NSString *szSub3 = [szMyString substringWithRange:range];       //number
  6. NSLog(@"%@",szSub3);
[cpp]  view plain copy

 
  1. //搜索与替换
  2. NSRange rangeSearch = [szMyString rangeOfString:@"is 5"];   //搜索
  3. if (rangeSearch.location != NSNotFound) {           //搜索不到是 NSNotFound
  4. //成功:rangeSearch.location;//位置 rangeSearch.length;//长度
  5. }
  6. NSLog(@"%@",[szMyString stringByReplacingCharactersInRange:rangeSearch      //用位置匹配替换
  7. withString:@"isn't 10"]);
  8. NSString *szReplaced = [szMyString stringByReplacingOccurrencesOfString:@" " withString:@"*"];  //匹配字符串替换
  9. NSLog(@"%@",szReplaced);
[cpp]  view plain copy

 
  1. //改变大小写
  2. NSLog(@"%@",[szMyString uppercaseString]);      //大写
  3. NSLog(@"%@",[szMyString lowercaseString]);      //小写
  4. NSLog(@"%@",[szMyString capitalizedString]);    //首字母大写
[cpp]  view plain copy

 
  1. //比较字符串
  2. NSString *sz1 = @"Hello World!";
  3. NSString *sz2 = @"Hello Mom!";
  4. if ([sz1 isEqualToString:sz2]) {/*相等*/}
  5. if ([sz1 hasPrefix:@"Hello"]) {NSLog(@"前部分相等");}        //从头开始比较
  6. if ([sz1 hasSuffix:@"d!"]) {NSLog(@"后部分相等");}       //从尾部比较
[cpp]  view plain copy

 
  1. //字符串转换数字
  2. NSString *szNumber = @"3.14";
  3. [szNumber intValue];
  4. [szNumber boolValue];
  5. [szNumber floatValue];
  6. [szNumber doubleValue];
[cpp]  view plain copy

 
  1. //可变字符串
  2. NSMutableString *szMuMyString = [NSMutableString stringWithString:@"Hello"];
  3. [szMuMyString appendFormat:@"World"];       //字符串,改变自身
  4. [szMuMyString uppercaseString];
  5. NSLog(@"%@",szMuMyString);

IOS NSString 用法详解的更多相关文章

  1. ios NSFileManager 用法详解

    转自:http://blog.csdn.net/ios_che/article/details/7287266 iPhone文件系统NSFileManager讲解是本文要介绍的内容,主要是通过ipho ...

  2. IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...

  3. UIWebView用法详解及代码分享

    今天我们来详细UIWebView用法.UIWebView是iOS内置的浏览器控件,可以浏览网页.打开文档等 能够加载html/htm.pdf.docx.txt等格式的文件. 用UIWebView我们就 ...

  4. iOS应用开发详解

    <iOS应用开发详解> 基本信息 作者: 郭宏志    出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...

  5. 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )

    原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理 ...

  6. iOS中-Qutarz2D详解及使用

    在iOS中Qutarz2D 详解及使用 (一)初识 介绍 Quartz 2D是二维绘图引擎. 能完成的工作有: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  7. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  8. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  9. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

随机推荐

  1. python opencv3 矩形 圆形边框

    git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 import numpy as np # 读入图像 ...

  2. python中的super( test, self).__init__()

    python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...

  3. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

  4. Gunicorn配置部分的翻译

    写在前面,虽然翻译得很烂,但也是我的劳动成果,转载请注明出处,谢谢. Gunicorn版本号19.7.1 Gunicorn配置 概述 三种配置方式 优先级如下,越后的优先级越大 1.框架的设置(现在只 ...

  5. 【转载】实现UTF8与GB2312编码格式相互转换(VC)已经验证!

    UTF-8编码:[1,1,1,0,A5,A6,A7,A8],[1,0,B3,B4,B5,B6,B7,B8],[1,0,C3,C4,C5,C6,C7,C8];对应的UNICODE编码:[A5,A6,A7 ...

  6. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  7. 传智播客PHP面试题宝典开放下载

    上下卷面试题更新完毕,一部让菜鸟4k+入职的 面试题宝典 http://php.itcast.cn/news/20130806/11490333788.shtml php视频教程 下载 http:// ...

  8. Tasker App Factory

    http://tasker.dinglisch.net/userguide/en/appcreation.html App Creation Introduction Hello World Exam ...

  9. systemtap 用户态调试3

    [root@localhost ~]# cat test.c #include <stdio.h> int main( void) { int a=0; a=fun(10,20); pri ...

  10. VS2010下配置Winpcap 开发环境

    http://blog.csdn.net/taotaoyouarebaby/article/details/27326829