• 字符串操作


    1、检测字符串中是否含有特殊字符

    -(BOOL)isIncludeSpecialCharact: (NSString *)str {

    //***需要过滤的特殊字符:~#&*<>《》()[]{}【】^@/¤|§¨「」『』¢¬ ̄~@#&*()——+|《》$_€。

        NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @"~#&*<>《》()[]{}【】^@/¤|§¨「」『』¢¬ ̄~@#&*()——+|《》$_€"]];

        if (urgentRange.location == NSNotFound)

        {        return NO;

        }    

        return YES;

    }

    2、过滤特殊字符

    在ios中 可以使用stringByTrimmingCharactersInSet函数过滤字符串中的特殊符号 

    首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号    

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""]; 

    由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的 

    然后调用stringByTrimmingCharactersInSet 

    NSString *trimmedString = [string stringByTrimmingCharactersInSet:set]; 

    trimmedString就是过滤后的字符串

    3、字符串格式化


    NSString *str = @"mobile developer tips";
     
    // Convert string to uppercase
    NSString *upperStr = [str uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSLog(@"upperStr: %@", upperStr);
     
    // Convert string to caps
    NSString *capStr = [upperStr capitalizedStringWithLocale:[NSLocale currentLocale]];
    NSLog(@"capStr: %@", capStr);
     
    // Convert string to lowercase
    NSString *lowerStr = [capStr lowercaseStringWithLocale:[NSLocale currentLocale]];
    NSLog(@"lowerStr: %@", lowerStr);
     
    输出内容如下:
     
    4、对url类型的字符串进行处理

    // 从路径中获得完整的文件名(带后缀)      

    exestr = [filePath lastPathComponent];  

        NSLog(@"%@",exestr);  

    // 获得文件名(不带后缀)  

    exestr = [exestr stringByDeletingPathExtension];      

        NSLog(@"%@",exestr);  

    // 获得文件的扩展类型(不带'.'  

        exestr = [filePath pathExtension];  

        NSLog(@"%@",exestr); 

    NSString *pathExtension = [path pathExtension];  

    pathExtension这个字符串的值将是“txt”。句点将被去掉了。如果没有句点指明扩展名,将返回一个空串。如果文件不存在,也将返回空串 

    iPhone中,在网络中的数据流中提取链接中的文件名称时,有很多方法,这里总结一些。  

    方法一:最直接。  

    NSString * urlString = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”;    

    NSString *fileName = [urlString lastPathComponent];   

    NSLog(@”%@”,fileName);  

    方法二:根据字符或者时字符串分割。  

    NSString *link = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”;    

    NSString *filename = [[NSString alloc] init];    

    NSArray *SeparatedArray = [[NSArray alloc]init];  

    SeparatedArray =[link componentsSeparatedByString:@"/"];  

    filename = [SeparatedArray lastObject];  

    NSLog(@”%@”,SeparatedArray);  

      

    NSLog(@”%@”,filename);  

    [filename release];  

    方法三:将链接看成路径。  

    NSString * urlString = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”;  

      

    NSArray *urlCom = [[NSArray alloc]initWithArray:[url pathComponents]];  

      

    NSLog(@”%@”,[urlCom lastObject]);  

      

    [urlCom release];  

      

    方法四:NSRange.它在截取二进制文件的时候十分方便。  

      

    NSString * urlString = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”;  

      NSString * fileName;  

      NSRange range = [urlString rangeOfString:@"/" options:NSBackwardsSearch];  

    if (range.location != NSNotFound)  

    {  

      

    fileName = [urlString substringFromIndex:range.location+1];  

    if([[fileName lowercaseString]hasSuffix:@”.gif”])  

    {  

    NSLog(@”%@”,fileName);  

    }  

    else  

    {  

    }    

    }     

    return;  

    }

  • 相关阅读:
    积少成多Flash(8) ActionScript 3.0 网页之获取参数,JavaScript与ActionScript之间的相互调用
    积少成多Flash(11) Flex 3.0 动画效果(effect)
    积少成多Flash(9) Flex 3.0 布局控件, 样式(css), 皮肤(skin)
    系出名门Android(8) 控件(View)之TextSwitcher, Gallery, ImageSwitcher, GridView, ListView, ExpandableList
    系出名门Android(9) 数据库支持(SQLite), 内容提供器(ContentProvider)
    积少成多 Flash(ActionScript 3.0 & Flex 3.0) 系列文章索引
    系出名门Android(5) 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock
    系出名门Android(1) 在 Windows 下搭建 Android 开发环境,以及 Hello World 程序
    小程序webview组件 nothing
    Python3的bytes/str之别
  • 原文地址:https://www.cnblogs.com/sgdkg/p/3049578.html
Copyright © 2020-2023  润新知