12

我的 json 正在返回这个:

地址=》城市路129号

\n肖尔迪奇

\n伦敦 EC1V 1JB";

我想删除“\n”。因为我想在一行中显示它。

我试过以下:

NSString * newReplacedString = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSString * newReplacedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

两者结果相同:

结果

密歇根大道北 900 号

芝加哥

伊利诺伊州 60611

如果我试过这个

NSString *s = [string stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];

结果

密歇根大道北 900 号

\n芝加哥

\n伊利诺伊州 60611

请建议。

4

11 回答 11

21

试试看 :

NSString *address = @"129 City Road \nShoreditch \nLondon EC1V 1JB";    
address = [address stringByReplacingOccurrencesOfString:@"\n" withString:@""];
address = [address stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

这有效,我得到了如下输出:

129 City Road Shoreditch London EC1V 1JB
于 2012-06-11T11:06:09.680 回答
16

看起来 the\n在你的字符串中是一个字面意思而不是换行符,那么你需要转义\,比如\\n

NSString * newReplacedString = [string stringByReplacingOccurrencesOfString:@"\\n" withString:@""];

NSString * newReplacedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
于 2012-06-11T11:11:24.273 回答
9
NSString *newString = [str stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];

对于那些可能正在寻找答案的人。尝试上述一次。它对我有用。

于 2014-05-13T10:17:10.700 回答
3

Priyanka Use Following code to remove new lines.

-(NSString *)stringByRemovingNewLinesAndWhitespace {

// Pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Strange New lines:
//  Next Line, U+0085
//  Form Feed, U+000C
//  Line Separator, U+2028
//  Paragraph Separator, U+2029

// Scanner
NSScanner *scanner = [[NSScanner alloc] initWithString:self];
[scanner setCharactersToBeSkipped:nil];
NSMutableString *result = [[NSMutableString alloc] init];
NSString *temp;
NSCharacterSet *newLineAndWhitespaceCharacters = [NSCharacterSet characterSetWithCharactersInString:
                                                  [NSString stringWithFormat:@" \t\n\r%C%C%C%C", 0x0085, 0x000C, 0x2028, 0x2029]];
// Scan
while (![scanner isAtEnd]) {

    // Get non new line or whitespace characters
    temp = nil;
    [scanner scanUpToCharactersFromSet:newLineAndWhitespaceCharacters intoString:&temp];
    if (temp) [result appendString:temp];

    // Replace with a space
    if ([scanner scanCharactersFromSet:newLineAndWhitespaceCharacters intoString:NULL]) {
        if (result.length > 0 && ![scanner isAtEnd]) // Dont append space to beginning or end of result
            [result appendString:@" "];
    }

}

// Cleanup
[scanner release];

// Return
NSString *retString = [[NSString stringWithString:result] retain];
[result release];

// Drain
[pool drain];

// Return
return [retString autorelease];

}

于 2012-06-11T11:17:59.433 回答
2
NSString *data= [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
于 2013-09-24T07:35:12.210 回答
1

要删除“\n”,请执行以下操作

//Str contains your json value
NSString *str;
NSString *newString = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
于 2012-06-11T11:02:10.050 回答
1
NSString *newString = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];

重复。在此处检查答案。

于 2012-06-11T11:11:47.747 回答
0

这对我有用:

NSString *s = [@"900 N Michigan \nAvenue \nChicago \nIllinois 60611" stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSLog(@"%@", s); //900 N Michigan Avenue Chicago Illinois 60611
于 2012-06-11T11:01:44.280 回答
0

它的工作

NSString *address = @"129 City Road"
    "\nShoreditch"
    "\nLondon EC1V 1JB";

    address = [address stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSLog(@"address--%@",address);
于 2012-06-11T11:04:49.957 回答
0

[string stringByReplacingOccurrencesOfString:@"\n" withString:@""]说的不行。。

你试过“\r”

 [string stringByReplacingOccurrencesOfString:@"\r" withString:@""]
于 2012-06-11T11:10:43.670 回答
0

在 Swift 2.2 中:让 newWord = address.stringByReplacingOccurrencesOfString("\n", withString: "")

于 2016-07-08T00:24:29.863 回答