码迷,mamicode.com
首页 > 其他好文 > 详细

字符串替换

时间:2017-03-26 20:33:41      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:logs   cte   tar   字符   class   ***   fst   com   nslog   

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|

 

字符串替换

标签:logs   cte   tar   字符   class   ***   fst   com   nslog   

原文地址:http://www.cnblogs.com/xufengyuan/p/6623874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!