-1

我像这样从 API 获取字符串

  New version of WhatsApp Messenger is
  now available.
  JANUARY 12, 2013
  Your upgrade will bring improvements, larger
  group chats and new features such as
  Profile Photos!

有没有办法从上面的字符串中获取段落?

我用了 NSArray *paragraphs = [result componentsSeparatedByString:@"\n"];

根据我的要求,它应该有 3 个字符串

 paragraphs=   {
          "New version of WhatsApp Messenger is now available."
          "JANUARY 12, 2013"
          "Your upgrade will bring improvements, larger  group chats and new features such as Profile Photos!"

    }

但它回来了

paragraphs= {
      "New version of WhatsApp Messenger is"
      "now available."
      "JANUARY 12, 2013"
      "Your upgrade will bring improvements, larger"
      "group chats and new features such as"
      "Profile Photos!"
 }  
4

3 回答 3

0

这是你的问题的一个黑客

1 检查所有换行符范围。

2 删除特定范围内的换行符。

3.完成

1.  int count = 0;
    for (unsigned int i=0; i < [result length]; ++i) {
        if ([result characterAtIndex:i] == '\n') {
            NSLog(@"index %i",i);
            ++count;

        }
    }

2.  

    NSRange first = {36, 1};
    NSRange fourth= {113,1};
    NSRange fifth={150,1};
    NSString *stringWithSpaces;

    stringWithSpaces = [result stringByReplacingCharactersInRange:first withString:@" "];
    result=stringWithSpaces;
    stringWithSpaces = [result stringByReplacingCharactersInRange:fourth withString:@" "];
    result=stringWithSpaces;
    stringWithSpaces = [result stringByReplacingCharactersInRange:fifth withString:@" "];
    result=stringWithSpaces;

     NSArray *paragraphs = [result componentsSeparatedByString:@"\n"];
     NSLog(@"Paragraph=%@",paragraphs);
  1. 完毕。

    快乐编码

于 2014-09-20T07:16:57.863 回答
0

你可以试试这个:

    NSArray* newLinestring = [result componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    NSMutableArray *newLinesArray = [NSMutableArray array];

   [result enumerateSubstringsInRange:NSMakeRange(0, result.length) options:NSStringEnumerationByParagraphs usingBlock:^(NSString *substring, NSRangesubstringRange, NSRange enclosingRange, BOOL *stop) {
  // ADD TO AN ARRAY HERE THE SUBSTRINGS
 }];
于 2014-09-17T10:46:07.577 回答
0

据我所知,输出是正确的,因为有 5 个换行符。输入不是你想要的。您的输入应该是:

New version of WhatsApp Messenger is now available.
JANUARY 12, 2013
Your upgrade will bring improvements, larger group chats and new features such as Profile Photos!
于 2014-09-17T11:54:53.880 回答