• NSString里面的数字


    0-9ASCII码:48 - 57

    方法一:

    //遍历NSString里面的数字

        NSString *str = @"ssdg0db9f567dazy";

        NSMutableString *strM = [NSMutableString string];

        for (int i = 0; i < str.length; i++) {

            unichar a =[str characterAtIndex:i];        

            if (!(a > 47 && a < 58) ) {

                //拼接字符串:方法一

                NSString *strResult = [str substringWithRange:NSMakeRange(i, 1)];

                [strM appendString:strResult];

                //拼接字符串:方法二

                //[strM appendFormat:@"%c",a];

            }

        }

      NSLog(@“%@",strM);

    方法二:利用正则表达式来实现

    #import "GJViewController.h"

    #import "NSString+Regex.h"

    @interface GJViewController ()

    @end

    @implementation GJViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    NSString *str = @"zhang23g114jn889"; 

        NSRegularExpression *regex = [NSRegularExpression

                                      regularExpressionWithPattern:@"\D*"

                                      options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators

                                      error:nil];

        NSArray *result = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];

        NSMutableString *strM = [NSMutableString string];

        for (NSTextCheckingResult *resultCheck in result) {

            if (resultCheck.range.length > 0) {

                [strM appendString:[str substringWithRange:resultCheck.range]];

            }

        }

        NSLog(@"%@",strM);

    }

  • 相关阅读:
    校验输入框输入两位小数
    限制输入框只能输入整数
    npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
    yum -y wget
    docker 安装mysql
    centos 安装docker
    VirtualBox安装CentOS系统
    Spring Cloud服务保护
    从零开始搭建系统2.8——HDFS安装及配置
    从零开始搭建系统4.3——详细设计
  • 原文地址:https://www.cnblogs.com/bluceZ/p/4629439.html
Copyright © 2020-2023  润新知