/--------操作字符串--NSString(静态字符串)---------------------

NSString * a = @"a+b+c+a+b+d";

NSArray  * m = [a componentsSeparatedByString:@"+"];//字符串根据某个字串拆分

NSRange x = [a rangeOfString:@"b"];//查找子字符串在总字符串的范围

NSLog(@"%lu, %lu", x.location , x.length);

NSString * r = [a stringByReplacingOccurrencesOfString:@"+" withString:@"-"];//把某个字符串替换成另一个字符串

NSLog(@"%@", r);

NSString * str = @"wo shi xiao hong jun!";

NSString * result = [str uppercaseString];//小写字母变成大写

NSLog(@"%@", result);

NSString * result1 = [str capitalizedString];//第一个单词变成大写

NSLog(@"%@", result1);

NSString * result2 = [result lowercaseString];//大写字母改为小写

NSLog(@"%@", result2);

NSString * Beijing= @"河南";        //字符串的声明

NSString * log=@"河南欢迎您a"; //[NSString stringWithFormat:@"I am '%@'", Beijing];     //字符串格式化

NSString * zhui = [Beijing stringByAppendingString:@"啦啦啦"];        //字符串追加

bool b=[Beijing isEqualToString:log];                               //字符串比较

NSString * hh = @"http://www.cnblog.com";

if([hh hasPrefix:@"http"]){                                          //查找以http开头的字符串

NSLog(@"含有http");

}else{

NSLog(@"没有http");
    }
    NSString * ss = @"123";

int a = [ss intValue];                                   //字符串转int型

double dd = [ss doubleValue];                          //字符串转double型

NSLog(@"%g", dd);

//字符串转数组

NSString * zifuchuan =@"one, two, three, four";

NSLog(@"string:%@", zifuchuan);

NSArray * array = [zifuchuan componentsSeparatedByString:@","];//用,把字符分成数字元素

//    NSLog(@"array:%@", array);                             //输出整个数组中所有元素

NSString * value = [array objectAtIndex:0];          //取出第0个元素

NSLog(@"value:%@", value);

//数组转字符串

NSString * zifuchuan2 = [array componentsJoinedByString:@","];//用,把各个数字组成字符串

NSLog(@"zifuchuan2:%@", zifuchuan2);
       
//-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)];//0带表下标,4表示长度

NSLog(@"string2:%@",string2);

//--------操作动态字符串--NSMutableString----------------------------------------------------

NSMutableString * mstr = [[NSMutableString alloc] init];

NSString * str1 = @"This is a example.";

//创建可变字符串

mstr = [NSMutableString stringWithString:str1];

//插入字符

[mstr insertString:@"very easy " atIndex:10];

//删除一些字符

[mstr deleteCharactersInRange:NSMakeRange(10,5)];

//查找并删除

NSRange substr = [mstr rangeOfString:@"example"];             //字符串查找,可以判断字符串中是否有

if (substr.location != NSNotFound) {

[mstr deleteCharactersInRange:substr];

}

//重新设置字符串

[mstr setString:@"This is string AAA"];

//替换字符串

[mstr replaceCharactersInRange:NSMakeRange(15, 2) withString:@"BBB"];   //从第15个字符串处替换掉后2个字符串
   
    //查找第一个并替换

NSString * search = @"This is";

NSString * replace = @"An example of";

substr = [mstr rangeOfString:search];

if (substr.location != NSNotFound) {

[mstr replaceCharactersInRange:substr withString:replace];      //把第1个遇到的substr替换为replace

NSLog(@"%@", mstr);
    }
   
    //查找全部匹配的,并替换

search = @"a";

replace = @"X";

substr = [mstr rangeOfString:search];

while (substr.location != NSNotFound) {

        [mstr replaceCharactersInRange:substr withString:replace];

        substr = [mstr rangeOfString:search];
    }

NSLog(@"%@", mstr);

OC中的那些String的更多相关文章

  1. java中的继承与oc中的继承的区别

    为什么要使用继承? 继承的好处: (1)抽取出了重复的代码,使代码更加灵活 (2)建立了类和类之间的联系 继承的缺点: 耦合性太强 OC中的继承 1.OC中不允许子类和父类拥有相同名称的成员变量名:( ...

  2. Swift: 比较Swift中闭包传值、OC中的Block传值

    一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...

  3. 关于OC中直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型

    关于OC直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型,我们完全可以把其转换为OC对象来进项打印调试,而不必对结构体中的成员变量进行打印.就好比我们可以使 ...

  4. OC中属性及方法

    1.声明式属性    a.实例变量    b.声明属性        自动生成setter/getter方法        .h ->@property 属性类型 属性名;        .m ...

  5. OC中@property属性关键字的使用(assign/weak/strong/copy)

    OC中@property属性关键字的使用(assign/weak/strong/copy) 一.assign 用于 ‘基本数据类型’.‘枚举’.‘结构体’ 等非OC对象类型 eg:int.bool等 ...

  6. iOS开发几年了,你清楚OC中的这些东西么!!!?

    iOS开发几年了,你清楚OC中的这些东西么!!!? 前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1 ...

  7. OC中的NSNumber、NSArray、NSString的常用方法

    和C语言不同,在Objective-C语言中,有单独的字符串类NSString.C语言中,string是由 char(ASCLL码)字符组成 OC中,字符串是由unichar(Unicode)字符组成 ...

  8. iOS开发几年了,你清楚OC中的这些东西么1

    前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1.2发布后就正式并且一直都使用了swift进行iOS ...

  9. oc中的类学习笔记1

    1.oc中的类和构造方法 NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init]; NSAutoreleasePool是一个类,alloc ...

随机推荐

  1. istringstream、ostringstream、stringstream 类简介

    本文系转载,原文链接:http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html ,如有侵权,请联系我:534624117@qq.co ...

  2. 火狐min-height不兼容解决方法

    我们在进行页面架构的时候,一般会分成三个section:header.body.footer.如下面这个页面: 这个时候我们就需要对body部分进行最小高度设置,才能避免footer部分出现在页面中间 ...

  3. ASP.NET知识总结(5.文件上传 文件下载)

    5.文件上传 ->说明:使用http协议只适合传输小文件,如果想传递大文件,则需要使用插件或者客户  端程序(使用ftp协议) ->客户端操作 <1>为表单添加属性:encty ...

  4. 使用MultipartEntity进行post请求的默认MIME类型

    MultipartEntity .FileBody的默认MIME类型:application/octet-stream

  5. ngx_image_thumb模块生成缩略图

    ngx_image_thumb是nginx中用来生成缩略图的模块. 编译前确定已经安装了libcurl-dev libgd2-dev libpcre-dev gd-devel pcre-devel l ...

  6. [CSS]Input标签与图片按钮对齐

    页面直接摆放一个input文本框与ImageButton图片按钮,但是发现没有对齐: <input type="text" id="txtQty" /&g ...

  7. 11.ok6410之led驱动程序编写

    led驱动程序编写 本文主要包含三部分,led驱动程序led.c编写,编译驱动程序的makefile的编写,以及使用驱动程序的应用程序led_app的编写 一.led.c编写 #include < ...

  8. Android UI 之实现多级列表TreeView

    所谓TreeView就是在Windows中常见的多级列表树,在Android中系统只默认提供了ListView和ExpandableListView两种列表,最多只支持到二级列表的实现,所以如果想要实 ...

  9. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

  10. HTML 表

    表格: <table></table> 插入一个表格 <tr></tr> 代表一行 其中插入<td></td>单元格       ...