zoukankan      html  css  js  c++  java
  • ios开发之--NSString的操作

    1.截取字符串

    NSString*string =@"sdfsfsfsAdfsdf";
    string = [string substringToIndex:7];//截取掉下标7之后的字符串
    NSLog(@"截取的值为:%@",string);
    [string substringFromIndex:2];//截取掉下标2之前的字符串
    NSLog(@"截取的值为:%@",string);

    2.匹配字符串

    NSString*string =@"sdfsfsfsAdfsdf";
    NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标
    NSLog(@"rang:%@",NSStringFromRange(range));
    string = [string substringWithRange:range];//截取范围类的字符串
    NSLog(@"截取的值为:%@",string);

    3.分隔字符串

    NSString*string =@"sdfsfsfsAdfsdf";
    
    NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
    NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf1.截取字符串
    
    NSString*string =@"sdfsfsfsAdfsdf";
    string = [string substringToIndex:7];//截取掉下标7之后的字符串
    NSLog(@"截取的值为:%@",string);
    [string substringFromIndex:2];//截取掉下标2之前的字符串
    NSLog(@"截取的值为:%@",string);

    4.分隔字符串,字符串分割成数组

    NSString*string =@"sdfsfsfsAdfsdf";
    
    NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
    NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf

    5,数组转成字符串

    NSArray *array = @[@"a",@"b",@"c",@"d",@"e",@"f"];
    NSString *string = [array componentsJoinedByString:@","];,为分隔符

    6,过滤字符串,过滤所有非字符符号

    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"[]{}(#%-*+=_)\|~(<>$%^&*)_+ "];
    NSString * hmutStr = [[mutStr componentsSeparatedByCharactersInSet: doNotWant]componentsJoinedByString: @""];
        
    NSLog(@"humStr is %@",hmutStr);
        
    return hmutStr;

    仅做记录!如有不妥之处,请大家不吝指教!

  • 相关阅读:
    原码、反码、补码,计算机中负数的表示
    [转]Vue 2.0——渐进式前端解决方案
    关于MySQL的tinyint(3)问题
    js对象的深拷贝及其的几种方法
    深入 js 深拷贝对象
    JS 数组克隆方法总结
    Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
    邮件措辞小计
    Forbidden You don't have permission to access / on this server PHP
    正则表达式
  • 原文地址:https://www.cnblogs.com/hero11223/p/7590348.html
Copyright © 2011-2022 走看看