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);

        }

  • 相关阅读:
    UVA 11235 Frequent Values ---RMQ
    UVA 12266 Stock prices --优先队列
    HDU 1896 Stones --优先队列+搜索
    POJ 1442 Black Box -优先队列
    POJ 2263 Heavy Cargo 多种解法
    POJ 3250 Bad Hair Day --单调栈(单调队列?)
    FZU1894 志愿者选拔 --单调队列
    POJ 2823 Sliding Window 再探单调队列
    UVA 11992 Fast Matrix Operations (二维线段树)
    两道相似KMP题
  • 原文地址:https://www.cnblogs.com/wudan7/p/3666322.html
Copyright © 2011-2022 走看看