Remove newline character from first line of NSString
cocoa, iphone, macos, nsstring, objective-c
Solution
This should do the trick:
NSString * ReplaceFirstNewLine(NSString * original)
{
NSMutableString * newString = [NSMutableString stringWithString:original];
NSRange foundRange = [original rangeOfString:@"\n"];
if (foundRange.location != NSNotFound)
{
[newString replaceCharactersInRange:foundRange
withString:@""];
}
return [[newString retain] autorelease];
}
Problem
How can I remove the first \n character from an NSString? Edit: Just to clarify, what I would like to do is: If the first line of the string contains a \n character, delete it else do nothing. ie: If the string is like this: ``` @"\nhello, this is the first line\nthis is the second line" ``` and opposed to a string that does not contain a newline in the first line: ``` @"hello, this is the first line\nthis is the second line." ``` I hope that makes it more clear.