首页 > 代码库 > 【学习ios之路:Object-C】NSString和NSArray

【学习ios之路:Object-C】NSString和NSArray

1.字符串

     *   字符串常用方法

     *   字符串创建方法(最常用)
     *   字符串追加方法
     *   求子串 以及字符串中取某个字符
     *   字符替换
     *   字符串与其他转换(经常用)
     *   字符串改变大小写

     *   字符串比较方法(经常用)

例如:

1).创建一个字符串对象方法:

	
  NSstring * str1 = @"hello"; //1.直接赋值  //2.通过initWithString  NSString * str = [[NSString alloc]initWithString:@"hello world!"];<pre name="code" class="objc">  //3.initWithUTF8String把C的字符串转化为OC的字符串
NSString * str_1 = [[NSString alloc]initWithUTF8String:"hello world"]; int a = 123;//用格式化字符串初始化 NSString * str_3 = [[NSString alloc]initWithFormat:@"a=%d%s%@", a,"abcd", @"efg"]; //可完成字符串的拼接以及完成C的字符串与OC的字符串的转化 NSString * str1 = [NSString stringWithString:@"hello world"]; NSString * str2 = [NSString stringWithUTF8String:"hello world"]; NSString * str3 = [NSString stringWithFormat:@"%s","hello world"];

 

2).字符串的转化

  NSString * str = @"hello";
 const char *p = [str UTF8String];将OC字符串转化为C的字符串
 NSString *str1 = @"123";
 int a = [str intValue];//将数字串转化成整型数据
 [str floatValue];//将数字串转化成float型
 [str doubleValue];//将数字串转化成double型
3).常用的字符串操作

	<pre name="code" class="objc">  //求字符串长度
  NSLog(@"%ld",[str1 length]);  
 
  //获取字符串中某个位置的字符
  NSLog(@"%c", [str1 characterAtIndex:1]);
 
  //比较两个字符串是否相等,相等返回YES(1),不相等返回NO(0)
  BOOL ret =  [str1 isEqualTo:str2];
 <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc">  //比较两个字符串的大小,str1大于 返回1  相等返回0 小于返回-1
  long ret1 = [str1 compare:str2];
 <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">  //不区分大小写比较字符串大小
  long ret2 = [str1 caseInsensitiveCompare:str2];
 <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">  //将字符串中的所有小写字符转换成大写 不改变原来的字符串
  NSString* ptr = [str2 uppercaseString];
 <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">  //将字符串中的所有大写字符转换成小写 不改变原来的字符串
  ptr = [str1 lowercaseString];
 <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">  //将字符串中出现的第一个字母转换成大写,其余字母小写

ptr = [str3 capitalizedString]; NSString* str4 = @"hello world";
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">   //查找子串,找不到返回NSNotFound 找到返回location和length
  NSRangerange = [str4 rangeOfString:@"wr"]; if (range.location != NSNotFound) { NSLog(@"%ld %ld",range.location, range.length); } NSString* str5 = @"helloworld";
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">   //字符串抽取 从下标0开始到4 不包括4
   NSString* ptr1 = [str5 substringToIndex:4];
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">     //从下标5开始抽取到字符串结束 包括5
   NSString* ptr2 = [str5 substringFromIndex:5];
      //NSRange range = NSMakeRange(4,3);
   NSRange range = {4,3};//结构体初始化
 <pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">//在range指定范围内抽取
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">//在range指定范围内抽取
//获取长度为3,从下标为4的字串 NSString* ptr3 = [str5 substringWithRange:range]; NSString* ptr4 = [str5 substringWithRange:NSMakeRange(4,3)]; NSString* str = @"www.1000phone.com";
//判断字符串是否是www开头
 BOOL ret = [str hasPrefix:@"www"]; NSString* str1 = @"1.txt";
//判断字符串是否是.txt结尾
  BOOL ret1 = [str hasSuffix:@".txt"]; //NSMutableString可变字符串(动态增加和减少的)
 //继承于NSString可以使用NSString的所有方法 //将不可变字符串转化为可变的字符串. NSMutableString* str = [[NSMutableStringalloc]initWithString:@"hello"];
//在指定下标(不越界)位置插入NSString类型字符串 [str insertString:@"123"atIndex:1];
//在字符串末尾追加字符串. [str appendString:@"123"];

 //从0下标开始删除2个字节 [str deleteCharactersInRange:NSMakeRange(0, 2)];

 //对字符串赋值,即修改可变字符串 [str setString:@"hehe"];
//将指定下标长度位置的字符串替换成IOS [str replaceCharactersInRange:NSMakeRange(3, 1) withString:@"ios"];
 


 

【学习ios之路:Object-C】NSString和NSArray