字符串替换

1.字符串的替换函数

  • - (NSString )stringByReplacingOccurrencesOfString:(NSString )target withString:(NSString *)replacement;
    • 用replacement替换target
    NSString *str = @"http:**520it.com*img*ljn.gif";
    NSString *newStr = [str stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
    NSLog(@"newStr = %@", newStr);

输出结果: http://www.520it.com/img/ljn.gif
  • - (NSString )stringByTrimmingCharactersInSet:(NSCharacterSet )set;

  去除首尾

    NSString *str =  @"   http://520it.com/img/ljn.gif   ";
    NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"str =|%@|", str);
    NSLog(@"newStr =|%@|", newStr);

输出结果:
str =|   http://520it.com/img/ljn.gif   |
newStr =|http://520it.com/img/ljn.gif|
    NSString *str =  @"***http://520it.com/img/ljn.gif***";
    NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];

    NSLog(@"str =|%@|", str);
    NSLog(@"newStr =|%@|", newStr);

输出结果:
str =|***http://520it.com/img/ljn.gif***|
newStr =|http://520it.com/img/ljn.gif|

 

posted @ 2017-03-26 20:22  iFat  阅读(186)  评论(0编辑  收藏  举报