Remove newline character from first line of NSString
Solution 1
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];
}
Solution 2
[string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]
will trim your string from any kind of newlines, if that's what you want.
[string stringByReplacingOccurrencesOfString:@"\n" withString:@"" options:0 range:NSMakeRange(0, 1)]
will do exactly what you ask and remove newline if it's the first character in the string
Solution 3
Rather than creating an NSMutableString and using a few retain/release calls, you can use only the original string and simplify the code by using the following instead: (requires 10.5+)
NSRange foundRange = [original rangeOfString:@"\n"];
if (foundRange.location != NSNotFound)
[original stringByReplacingOccurrencesOfString:@"\n"
withString:@""
options:0
range:foundRange];
(See -stringByReplacingOccurrencesOfString:withString:options:range:
for details.)
The result of the last call method call can even be safely assigned back to original IF you autorelease what's there first so you don't leak the memory.
Brock Woolf
I predominantly code in Objective-C (Cocoa Touch) and C# (.net) I write games and utility software and do iPhone and iPad development. More here: BrockWoolf.com.
Updated on July 24, 2020Comments
-
Brock Woolf almost 4 years
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.
-
Brock Woolf almost 15 yearsoptions:nil should be options:0
-
monowerker almost 15 yearsnil == NULL == 0 in C++. nil == NULL == (void *)0 in C. NSStringCompareOptions is typedef NSUInteger which is 32-bit on 32-bit platforms and 64-bit on 64 bit platforms. Integer literals in place of named constants go against my sensibilities ;)
-
monowerker almost 15 yearsYou're right, I would cast it to (NSStringCompareOptions)nil for quick lookup of the masks and remove the warning. But 0 is never going to be wrong.
-
Quinn Taylor almost 15 yearsThis has the downside of either (1) returning a mutable string or (2) requiring an immutable copy to avoid that. Use -stringByReplacingOccurrencesOfString:withString:options:range: to do this more efficiently. My answer includes details and a code sample.
-
Quinn Taylor almost 15 yearsExcellent catch on 10.5+ for this method, I'd forgotten that. I'm lucky enough to not have to worry about Tiger support anymore. :-)
-
GameLoading about 13 years[string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] is right.. Thanks