/--------操作字符串--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. Swift - UITableView里的cell底部分割线左侧靠边

    override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...

  2. wap

    1.wap下拉刷新丑陋版 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  3. 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number

    题目描述: Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...

  4. linux日志文件

    linux日志文件 在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. /var/log/messages — 包括整体系统信息,其中也包含系统启动期间的 ...

  5. csc.rsp Nuget MVC/WebAPI、SignalR、Rx、Json、EntityFramework、OAuth、Spatial

    # This file contains command-line options that the C# # command line compiler (CSC) will process as ...

  6. JS控制按钮不能连续被点击

    将下面代码拷贝进一个html文件中就可以运行查看效果. function downLoad(evt) { disabledButton(); MyPeriodicalExecuter(evt); } ...

  7. C#中的Lambda表达式的演化过程

    原文:http://www.cnblogs.com/zhaopei/p/5767631.html

  8. NOIP2009分数线划定【B004】

    [B004]分数线划定[难度B]—————————————————————————————————————————————————————————————————————————— [题目要求] 世博 ...

  9. T-SQL Recipes之生成动态列表数据

    Problem 首先什么是动态列表?举个示例,假设你想输出以逗号分隔的IDs,如: 1,45,67,199,298 Solution 生成动态列表数据在我们生活场景中很常见,比如在 Dynamic P ...

  10. MongoDB-权限配置

    启动 Mongo\bin\mongo.exe1.添加>use admin #切换到MongoDB数据库的用户表>db.addUser("Name","Pass ...