zoukankan      html  css  js  c++  java
  • 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);

    }

  • 相关阅读:
    javascript04 引用类型
    apache httpd.conf 配置信息
    服务器集群原理
    网站优化常用技术
    php垃圾回收
    smarty2 简单应用 小留言板
    C# DataGridView 编辑单元格保存时防止单元格在编辑状态而产生空值
    C# 把字符串类型日期转换为日期类型
    C# Split方法操作取出文本文档txt文件里数据
    WinForm OR ASP.NET获取修改配置文件config的节点
  • 原文地址:https://www.cnblogs.com/bluceZ/p/4629439.html
Copyright © 2011-2022 走看看