风的研发日记


欢迎来到我的个人站~


NSString 相关笔记

  • 1.截取字符串

     NSString*string =@"sdfsfsfsAdfsdf";
     string = [string substringToIndex:7];//截取掉下标7之后的字符串
     NSLog(@"截取的值为:%@",string);
     [string substringFromIndex:2];//截取掉下标2之前的字符串
     NSLog(@"截取的值为:%@",string);
    
  • 2.匹配字符串

      NSString*string =@"sdfsfsfsAdfsdf";
      NSRange range = [string rangeOfString:@"f"];//匹配得到的下标
      NSLog(@"rang:%@",NSStringFromRange(range));
      string = [string substringWithRange:range];//截取范围类的字符串
      NSLog(@"截取的值为:%@",string);
    
  • 3.分隔字符串

      NSString*string =@"sdfsfsfsAdfsdf";
    	
      //从字符A中分隔成2个元素的数组
      NSArray *array = [string componentsSeparatedByString:@"A"]; 
    	
      //结果是adfsfsfs和dfsdf
      NSLog(@"array:%@",array); 
    	
      NSRange range = [model.serviceName rangeOfString:@"★"];
    	
      //字符串中没有★,名字不可能相同
      if (range.location==NSNotFound) return;
    	
      /**截取*/
      NSString*codeString=[model.serviceName substringToIndex:range.location];
    
  • 判断字符串中是否包含某个字符

      - (void)string:(NSString *)string containsStr:(NSString *)str{
    
          BOOL isContain=([string rangeOfString:str].location !=NSNotFound)?YES:NO;
          return isContain;
      }
    
  • 从 URL中获取文件名以及后缀

      NSString* index=@"/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip";
    
      //对路径截取的9种操作
    
      NSLog(@"1=%@",[index lastPathComponent]);
    
      NSLog(@"2=%@",[index stringByDeletingLastPathComponent]);
    
      NSLog(@"3=%@",[index pathExtension]);
    
      NSLog(@"4=%@",[index stringByDeletingPathExtension]);
    
      NSLog(@"5=%@",[index stringByAbbreviatingWithTildeInPath]);
    
      NSLog(@"6=%@",[index stringByExpandingTildeInPath]);
    
      NSLog(@"7=%@",[index stringByStandardizingPath]);
    
      NSLog(@"8=%@",[index stringByResolvingSymlinksInPath]);
    
      NSLog(@"9=%@",[[index lastPathComponent] stringByDeletingPathExtension]);
    	
      //对应结果
    	
      1=2013_50.zip
    
      2=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books
    
      3=zip
    
      4=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50
    
      5=~/Documents/DownLoad/books/2013_50.zip
    
      6=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip
    
      7=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip
    
      8=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip   9=2013_50
    
  • 字符串的转换

      1. NSString转化为UNICODE String:
      (NSString*)fname = @“Test”;
      char fnameStr[10];
      memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
    
      //与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节
    
      2. NSString 转化为 char *
      NSString * str= @“Test”;
      const char * a =[str UTF8String];
    
      3.char * 转化为 NSString
      NSString *str=[NSString stringWithCString  encoding:NSUTF8StringEncoding];
    
      4. char * 转化 NSData
      //方法一:
      char * a = (char*)malloc(sizeof(byte)*16);
      NSData *data = [NSData dataWithBytes: a   length:strlen(a)];
      //方法二:
      转换为NSString: - (id)initWithUTF8String:(const char *)bytes
      然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
    
      5. NSData 转化 char *
      NSData data ;
      char* a=[data bytes];
    
      6. NSData 转化 NSString;
      NSData* data;
      NSString* aStr= [NSString alloc] initWithData:data   encoding:NSASCIIStringEncoding];
    
      7. NSString 转化 NSData对象
      NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
    
      8.NSString 转化 NSURL
      NSString *urlString=[@"http://www.google.com/search?client=safari&rls=en&q=搜索&ie=UTF-8&oe=UTF-8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
      9.NSURL 转化 NSString
      NSURL *url=[NSURL URLWithString:urlString];
      NSString *s=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
  • NSString 文字编码转换

      NSURL *url = [NSURL URLWithString:urlStr];
      NSData *data = [NSData dataWithContentsOfURL:url];
      NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
      NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
    
最近的文章

图像相关

获得屏幕图像+ (UIImage *)imageFromView: (UIView *) theView{ UIGraphicsBeginImageContext(theView.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); [theView.layer renderInContext:context]; UIImage *theImage = UIGraphicsGetImag...…

iOS 开发笔记继续阅读
更早的文章

UITableView 开发小技巧

隐藏 UITableView的滚动条以及修改滚动条的颜色 //隐藏滚动条 self.tableView.showsVerticalScrollIndicator = NO; //修改颜色 self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite; UITableView 上拉时多显示一部分区域 //设置 TableView 的底部 View UIView *footVi...…

iOS继续阅读