views:

3879

answers:

3

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.

+10  A: 
[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

monowerker
options:nil should be options:0
Brock Woolf
nil == 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
@monowerker: This isn't C++. I realise that nil is simply defined as zero. The Xcode compiler for some reason in 3.0 gives you a warning, using 0 fixes this. Weird, but that's what happens.
Brock Woolf
You'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.
monowerker
+4  A: 

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];
}
e.James
This 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
+4  A: 

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.

Quinn Taylor
+1 This is much better than my solution. That particular method is only available in version 10.5 and up, but that should be most systems by now, what with 10.6 coming out soon.
e.James
Excellent catch on 10.5+ for this method, I'd forgotten that. I'm lucky enough to not have to worry about Tiger support anymore. :-)
Quinn Taylor