zoukankan      html  css  js  c++  java
  • 字符串替换

    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|
  • 相关阅读:
    闭包函数 (字符编码,文件处理,函数基础总结)
    函数参数详解
    文件处理及函数基础
    文件处理高级
    面向对象----反射
    正则表达式与re模块
    常用模块
    模块和包
    内置函数与匿名函数
    HDU
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6623874.html
Copyright © 2011-2022 走看看