A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© czylwq520 中级黑马   /  2015-9-28 23:28  /  590 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  NSString *Beijing= @"北京欢迎您";               //字符串的声明
       NSString *log=@"北京欢迎您a"; //[NSString stringWithFormat:@"I am '%@'",Beijing];         //字符串格式化
       NSString *zhui = [Beijing stringByAppendingString:@"哈哈哈"];               //字符串追加
       bool b=[Beijing isEqualToString:log];                                                             //字符串比较
       NSString *hh = @"http://www.sina.com.cn";
       if([hh hasPrefix:@"http"]){                                                                                   //查找以http开头的字符串
               NSLog(@"含有http");
       }else{
               NSLog(@"没有http");
       }
       NSString *ss = @"123";
       int a = [ss intValue]+13;                                                                     //字符串转int型
       double dd = [ss doubleValue]+33.3;                                                   //字符串转double型
       NSLog(@"%g",dd);
//字符串转数组
       NSString *zifuchuan =@"one,two,three,four";
       NSLog(@"string:%@",zifuchuan);
       NSArray *array = [zifuchuan componentsSeparatedByStr ing:@","];
//       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)];
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个字符串
      

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马