码迷,mamicode.com
首页 > 移动开发 > 详细

iOSDay13之NSString

时间:2016-03-23 21:53:02      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:

C语言中,字符串是有char(ASC||码)字符组成。

OC中,字符串是由unichar(Unicode)字符组成。

1、字符串(NSString)

 NSString不可变字符串,即:创建以后,内容和长度不能更改

 NSMutableString可变字符串即:创建以后,内容和长度可以修改

2、不可变字符串创建

 1> 初始化方法创建对象

  - (instancetype)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

   1 NSString *string1= [[NSString alloc] initWithFormat:@"hello wold"];                 

 2> 便利构造器创建对象

  + (instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

   1 NSString *string2 = [NSString stringWithFormat:@"hell wold"];                       

 3> 字面量创建对象

   1 NSString *string3 = @"hello kitty";                                      

3、不可变字符串常用方法

 1> 获取字符串的长度

  @property (readonly) NSUInteger length;

  实现方式:str.length;

 2> 获取指定位置的字符  unichar OC 中用来表示字符

  - (unichar)characterAtIndex:(NSUInteger)index;

unichar c= [string2 characterAtIndex:0];

NSLog(@"c =%c",c);

 3> 字符串比较

  ① 比较

   - (NSComparisonResult)compare:(NSString *)string;

1   NSString *s12 = @"zhaofuyi";
2   NSString *s13 = @"wanghai";
3   NSInteger result = [s12 compare:s13];
4   NSLog(@"result = %ld", result);

  ② 字符串是否相等

   - (BOOL)isEqualToString:(NSString *)aString;

BOOL isEqual = [s12 isEqualToString:s13];
NSLog(@"isEqual = %d", isEqual);

 4> 拼接字符串

  ① - (NSString *)stringByAppendingString:(NSString *)aString;

1        NSString *string8 = @"nana";
2        NSString *string9 = @"liuaoran";
3        NSString *stringlove = [string8 stringByAppendingString:string9];
4        NSLog(@"stringlove=%@",stringlove);

  ② - (NSString *)stringByAppendingFormat:(NSString *)format, ...;

 5> 替换字符串

  ① - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement;

    NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len);  // 确定位置和长度

  ② - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

1 NSString *string10 = @"李易峰";
2         
3 NSString *string11 = [string10 stringByReplacingOccurrencesOfString:string10 withString:@"赵符壹"];
4         
5 NSLog(@"nan=%@",string11);

 6> 字符串对象转化为其他类型

  @property (readonly) double doubleValue;
  @property (readonly) float floatValue;
  @property (readonly) int intValue;
  @property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);
  @property (readonly) long long longLongValue NS_AVAILABLE(10_5, 2_0);
  @property (readonly) BOOL boolValue NS_AVAILABLE(10_5, 2_0);

1         NSString *s14 = @"123";
2         NSInteger strValue = [s14 integerValue];
3         NSLog(@"strValue = %ld", strValue);

 7> 判断字符串的前缀和后缀

  - (BOOL)hasPrefix:(NSString *)str; // 前缀
  - (BOOL)hasSuffix:(NSString *)str; // 后缀

1 NSString *string4 = @"www.欧巴.png";  // 图片欧巴
2      
3 BOOL isHassuffix = [string4 hasSuffix:@".png"];  // 后缀
4 NSLog(@"isHassuffix=%d",isHassuffix);
5           
6 BOOL ishasprefix = [string4 hasPrefix:@"www."];
7 NSLog(@"ishasprefix = %d",ishasprefix);  // 前缀

 8> 查找某个字符串在另一个字符串的范围

  - (NSRange)rangeOfString:(NSString *)searchString;

1      NSString *string5 = @"蓝鸥科技有限公司";
2      NSString *string6 = @"蓝鸥科技";
3         
4      // NSRange 结构体,表示成员变量
5      // location   起始位置
6      // length  长度
7      NSRange range =[string5 rangeOfString:string6];
8      NSLog(@"location=%ld,length = %ld",range.location,range.length);

 9> 字符串截取

  - (NSString *)substringWithRange:(NSRange)range;

1 NSString *string7 = @"hello wold kitty";
2         
3 NSRange range1 = NSMakeRange(0, 5);
4 NSString *sunstr1 = [string7 substringWithRange:range1];
5 NSLog(@"sunstr1=%@",sunstr1);

  从指定位置向后截取

  - (NSString *)substringFromIndex:(NSUInteger)from;

  从指定位置向前截取,不包含指定位置的下标

  - (NSString *)substringToIndex:(NSUInteger)to;

1         // 从指定位置向后截取
2         NSString *sunstr2 = [string7 substringFromIndex:12];
3         NSLog(@"sunstr2=%@",sunstr2);
4         // 从指定位置向前截取,不包含指定位置的下标
5         NSString *sunstr3 = [string7 substringToIndex:5];
6         NSLog(@"sunstr3=%@",sunstr3);

 10> 字符串大小写操作

  ① 字符串全部大写

   @property (readonly, copy) NSString *uppercaseString;

  ② 字符串全部大写

   @property (readonly, copy) NSString *lowercaseString;

  ③ 字符串首字母大写

   @property (readonly, copy) NSString *capitalizedString;

 1         // 首字母大写
 2         NSString *s15 = @"zheng";
 3         s15 = [s15 capitalizedString];
 4         NSLog(@"s15 = %@", s15);
 5         // 全部大写
 6         s15 = [s15 uppercaseString];
 7         NSLog(@"s15 = %@", s15);
 8         // 全部小写
 9         s15 = [s15 lowercaseString];
10         NSLog(@"s15 = %@", s15);

4、可变字符串(NSMutableString)创建

 1> 可变与不可变的区别

  不可变字符串 —— 本身不能被修改。

  对不可变字符串的修改操作,操作的是原字符串副本,得到的是一个新的字符串。 

  可变字符串 —— 本身被修改。

  可变字符串修改的是原字符串,因此可变字符串的操作方法没有返回值

 2> 初始化方法创建对象

  - (NSMutableString *)initWithString:(NSString *)format;

 3> 便利构造器创建对象

  + (NSMutableString *)stringWithString:(NSString *)format;

1         // 初始化
2         NSMutableString *ms1 = [[NSMutableString alloc] initWithFormat:@"Hello world"];
3         // 便利构造器
4         NSMutableString *ms2 = [NSMutableString stringWithFormat:@"Hello kitty"];

5、NSMutableString方法

 1> 拼接字符串

  - (void)appendFormat:(NSString *)format, ...;

 2> 插入字符串

  - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;

 3> 删除字符串

  - (void)deleteCharactersInRange:(NSRange)range;

 4> 替换字符串

  - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 

 5> 重置字符串

  - (void)setString:(NSString *)aString;

 1 // 1.拼接字符串
 2 NSMutableString *ms3 = [NSMutableString stringWithFormat:@"-1"];
 3 [ms3 appendString:@"软件38"];
 4 NSLog(@"ms3 = %@", ms3);
 5 // 2.插入字符串
 6 [ms3 insertString:@"," atIndex:2];
 7 NSLog(@"ms3 = %@", ms3);
 8  // 3.删除字符串
 9 NSRange range4 = NSMakeRange(3, 2);
10 [ms3 deleteCharactersInRange:range4];
11 NSLog(@"ms3 = %@", ms3);
12 // 4.字符串替换
13 NSMutableString *ms4 = [NSMutableString stringWithFormat:@"赵符以"];
14 [ms4 replaceCharactersInRange:NSMakeRange(1, 2) withString:@"-1"];
15 NSLog(@"ms4 = %@", ms4);
16 // 5.重置字符串
17 [ms4 setString:@"ruanjian38"];
18 NSLog(@"ms4 = %@", ms4);

 

iOSDay13之NSString

标签:

原文地址:http://www.cnblogs.com/gfxxbk/p/5311821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!