NSString

content:

  • length:NSUInteger,%ld
  • characterAtIndex: it isn't get the string but the unichar
NSString *string = @"Abhd";
    NSLog(@"%hu",[string characterAtIndex:0]);
    unichar ch = [string characterAtIndex:0];
    if (ch == 'A') {
        NSLog(@"%u",ch);
    }
NSString *string1 = @"A String"; 
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大写
NSLog(@"string2:%@",[string2 lowercaseString]);//小写 
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string"; 
NSString *string2 = [string1 substringToIndex:3]; 
substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string"; 
NSString *string2 = [string1 substringToIndex:3]; 
substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string"; 
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
rangeOfString: //查找字符串某处是否包含其它字符串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
NSInteger location = range.location;
NSInteger leight = range.length;
NSString *astring = [NSString stringWithFormat:@"Location:%ld,Leight:%ld",location,leight]];
NSLog(@"astring:%@",astring);
附:
NSRange的两个属性:
location,
length
-appendString: and -appendFormat: //把一个字符串接在另一个字符串的末尾
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"hello"];
[String1 appendString:@",Liang Girl 11"];
NSString *string2 = @",Liang Girl 22";
[String1 appendFormat:@"%@",string2];
[String1 appendFormat:@",Liang Girl 33"];
NSLog(@"String1:%@",String1);
-insertString: atIndex: //在指定位置插入字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
-setString: 
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 
[String1 setString:@"Hello Word!"]; 
NSLog(@"String1:%@",String1);
-replaceCharactersInRange: withString: //用指定字符串替换字符串中某指定位置、长度的字符串
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; 
NSLog(@"String1:%@",String1);
-hasPrefix: //检查字符串是否以另一个字符串开头
- hasSuffix:
NSString *String1 = @"NSStringInformation.txt"; 
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); [String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
从字符串分割到数组- componentsSeparatedByString:
NSString *string = @"One,Two,Three,Four";
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
从数组合并元素到字符串- componentsJoinedByString:
NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
查找与替换
-(NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement
在给定的范围内查找并替换
-(NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
按照所给出的范围替换的原有的字符
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; // 用于NSMutableString 
NSLog(@"String1:%@",String1);

扩展:

  • stringByReplacingCharactersInRange
以前解析html的时候,不知道该怎么做,勉强做出来的效果还不好,以后可以用这个试试了。
摘自:http://blog.csdn.net/yqmfly/article/details/12944391
在做标签过滤的时候,用了如下代码,结果内存几秒钟就达到饱满了。
while ((ran = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
            str = [str stringByReplacingCharactersInRange:ran withString:@""];
        }
后来修改了一下,把str拆分了,内存增长就没那么快了。
贴一下网页标签过滤的代码。
- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
    NSRange ran;
    NSArray *arrMyStr = [originHtmlStr componentsSeparatedByString:@"\n"];
    NSMutableString *strOrg = [NSMutableString string];
    for (int iArr=0;iArr < [arrMyStr count] ;iArr++) {
        NSString *strArr = [arrMyStr objectAtIndex:iArr];
        while ((ran = [strArr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
strArr = [strArr stringByReplacingCharactersInRange:ran withString:@""];
        }
        if (strArr.length > 0) {
            [strOrg appendString:strArr];
        }
    }
    return strOrg;
}
  • rangeOfString
摘自:http://blog.sina.com.cn/s/blog_7b9d64af01019q2m.html
在查资料的时候,无意间发现了一个好方法:组合URL。其中,用到了rangeOfString方法:查找字符串。那就手下把:
-(NSURL*)smartURLForString:(NSString*)str
{
  NSURL *   result;
  NSString * trimmedStr;
  NSRange   schemeMarkerRange;
  NSString * scheme;
   
  result = nil;
   // 去除空白
  trimmedStr = [strstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
  if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
    schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
     
    if(schemeMarkerRange.location ==NSNotFound) {
      result =[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", trimmedStr]];
    } else {
      scheme =[trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
      assert(scheme != nil);
       
       if( ([scheme compare:@"http"  options:NSCaseInsensitiveSearch] == NSOrderedSame)
       || ([schemecompare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
        result = [NSURL URLWithString:trimmedStr];
      }else {
         //It looks like this is some unsupported URL scheme.
      }
    }
  }
   
  return result;
}
  • 如何打印NSRange
NSLog(@"%@",NSStringFromRange(range));
同类技巧:使用对应的转换
NSStringFromCGPoint   
NSStringFromCGSize   
NSStringFromCGRect  
NSStringFromCGAffineTransform   
NSStringFromUIEdgeInsets

推荐阅读更多精彩内容