zoukankan      html  css  js  c++  java
  • 从字符串中找到成对出现的符号之间的子字符串

    如:aaabbs[e]yuejiashidi[/e]suehahs[e]ueha[/e]lsuehahs[e] [/e]

    则输出:yuejiashidi      

                ueha

                ueha

    //装载每一对[e][/e]之间的字符串

        NSMutableArray *subArray = [NSMutableArraynew];

        //表示当前这对[e][/e]的字符串,如[e]yuejiashidi[/e],则它表示为yuejiashidi

        NSMutableString *subString = [NSMutableStringnew];

        //原字符串

        NSMutableString *str = [[NSMutableStringalloc] initWithFormat:@"%@", @"aaabbs[e]yuejiashidi[/e]suehahs[e]ueha[/e]lsuehahs[e]ueha[/e]"];

        //遍历这条原字符串

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

            char ch = [str characterAtIndex:i];

            //如果这条字符串是以[e]开头

            if ([str hasPrefix:@"[e]"]) {

                char subCh = [str characterAtIndex:i + 3];   //得到[e]后的字符,

                if (subCh == '[') {

                    char nextCh = [str characterAtIndex:i + 1 + 3];

                    if (nextCh == '/') {

                        NSString *str1 = [NSString stringWithString:subString];

                        [subArray addObject:str1];

                        

                        [str deleteCharactersInRange:NSMakeRange(0, i + 3 + 4)];

                        i = -1;

                        [subString setString:@""];

                        

                    }else{

                        [subString appendFormat:@"%c", subCh];

                    }

                }else{

                    [subString appendFormat:@"%c", subCh];

                }

            }else{

                if (ch == '[') {

                    [str deleteCharactersInRange:NSMakeRange(0, i)];

                    i = -1;

                }

            }

            

        }

        for (NSString *sub in subArray) {

            NSLog(@"=sub  %@",sub);

        }

  • 相关阅读:
    python---基础知识回顾(九)图形用户界面-------Tkinter
    python---基础知识回顾(八)数据库基础操作(sqlite和mysql)
    python---重点(设计模式)
    python---基础知识回顾(七)迭代器和生成器
    python---权限管理和菜单生成
    python---基础知识回顾(六)网络编程
    python---基础知识回顾(五)(python2.7和python3.5中的编码)
    CustomProgressBar
    android-square-progressbar-legacy
    AnimImageView
  • 原文地址:https://www.cnblogs.com/wudan7/p/3666322.html
Copyright © 2011-2022 走看看