dugulong

现在需要对文章的段落进行重新编辑,将文章的每一个段落的首行缩进8个空格。

首先要做的是,将文章的段落回车符进行统一,因为回车符号有以下几种:"/r/n","/n","/r"。这几种符号都可以使段落进行回车换行。只有首先统一了回车符号,才便于以后对段落进行区分然后进行编辑处理。

-(void)removeReturnKey:(int)saveNumber{

    NSError *error;

    NSString *textFileContents = [NSStringstringWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"18_1"ofType:@"txt"]encoding:NSUTF8StringEncodingerror: & error];//读取文章内容

 

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

    NSUInteger len = [textFileContents length];

    NSMutableString *text = [NSMutableStringstringWithCapacity:len];//转换为可编辑的字符串

    

    NSLog(@"txt11===%@", text);

    for (int i=0; i<[textFileContents length]; i++) {

        unichar ch = [textFileContents characterAtIndex:i];//逐个字符进行扫描

        NSLog(@"ch===%d",ch);

        if (ch==L\'\n\'||ch==\'\n\') {//n’转换为‘/r’

            NSLog(@"发现一次\n");

            unichar sh = [textFileContents characterAtIndex:i-1];

            if (sh==L\'\r\'||ch==\'\r\') {

                continue;//如果前面的一个字符为r’,就把前面的一个字符删除

            }else {

                ch = \'\r\';//如果前面没有r’,就把n’替换为‘/r’

            }

        }

        [text appendString:[NSStringstringWithCharacters:&ch length:1]];

    }

    

    NSLog(@"text====%@",text);//这个时候得到的新内容就是经过处理的内容

}

这样我们得到的文章就是统一了回车换行符的文章,可以利用ios自带的函数取出每一个段落存放在数组中。

 NSArray *lines = [textFileContents componentsSeparatedByString:@"\r"];

    NSLog(@"Number of lines in the file:%lu", [lines count]  );

       NSString *fristStr = [lines objectAtIndex:0];

     for (int i=1; i<[lines count]; i++) {

         NSString *str = [lines objectAtIndex:i];

         NSLog(@"str==%ld",[str length]);

    int emptyNumber=0;

        if ([str length]<1) {

            fristStr = [fristStr stringByAppendingString:@"\r"];

            fristStr = [fristStr stringByAppendingString:str];

            continue;

        }

        

 

    int strlength = [str length];

        NSLog(@"strlength==%d",strlength);

 

        for (int i = 0; i<strlength; i++) {

           unichar ch = [str characterAtIndex:i];

            if (ch == L\' \' || ch == 12288) {

                    emptyNumber++;

            }

        }  

    NSLog(@"现在的空格数目位==%d",emptyNumber);

    

    

    //如果不够8个空格凑够8个空格

    NSMutableString *dstStr = [NSMutableStringstringWithString:str];

    

    if (emptyNumber<8) {

        for (int i =0; i<8-emptyNumber; i++) {

            [dstStr insertString:@" " atIndex:0];

            NSLog(@"10101010");

        }

    }

    

    fristStr = [fristStr stringByAppendingString:@"\r"];

    fristStr = [fristStr stringByAppendingString:dstStr];

    NSLog(@"sdafasd==%@",fristStr);

   }

//    [self saveData:fristStr];

   这个时候得到的  fristStr就是处理之后的格式了。然后存在磁盘上就可以了 

    }

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2021-12-19
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-30
  • 2021-12-04
  • 2022-12-23
  • 2021-05-28
  • 2021-11-05
  • 2021-11-25
相关资源
相似解决方案