美文网首页iOS基础学习
Objective-C基础学习之字符串替换

Objective-C基础学习之字符串替换

作者: WenJim | 来源:发表于2017-10-23 02:05 被阅读7次

1.字符串的替换函数

  • -(NSString )stringByReplacingOccurrencesOfString:(NSString )target withString:(NSString *)replacement;

    • 用replacement替换target
    NSString *str = @"http:&&www.xiaoningle.com&img&logo.png";
    NSString *newStr = [str stringByReplacingOccurrencesOfString:@"&" withString:@"/"];
    NSLog(@"newStr = %@", newStr);

输出结果: http://www.xiaoningle.com/img/logo.png
  • -(NSString )stringByTrimmingCharactersInSet:(NSCharacterSet )set;
    • 去除首尾
    NSString *str =  @"    http://www.xiaoningle.com/img/logo.png   ";
    NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"str =|%@|", str);
    NSLog(@"newStr =|%@|", newStr);

输出结果:
str =|    http://www.xiaoningle.com/img/logo.png   |
newStr =|http://www.xiaoningle.com/img/logo.png|
    NSString *str =  @"***http://www.xiaoningle.com/img/logo.png***";
    NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];

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

输出结果:
str =|***http://www.xiaoningle.com/img/logo.png***|
newStr =|http://www.xiaoningle.com/img/logo.png|

相关文章

网友评论

    本文标题:Objective-C基础学习之字符串替换

    本文链接:https://www.haomeiwen.com/subject/sokouxtx.html