user1833903
user1833903

Reputation: 197

Remove BackEnd character in a 'NSString'

I'm facing a problem when I try to remove a character in a 'NSString'. The character is a backend (\n).

My 'NSString' is for example like this :

My text is
also in a second line

And I want to get all in one line like this :

My text is also in a second line

The problem is I don't know how to change this... I tried to locate the '\n' characters with a loop :

for (int delete = 0; delete < myString.length; delete++)
{
    if ([myString characterAtIndex:delete] == 10)
    {
        [myString stringByReplacingCharactersInRange:NSMakeRange(delete,0) withString:@" "];
    }
}

Or things like :

myString = [myString stringByReplacingOccurrencesOfString:@"\r" withString:@" "];

(I see that \r could be the backend in a nslog...)

Nothings work..

Thank you for your help in advance !

Upvotes: 0

Views: 56

Answers (1)

Earl Grey
Earl Grey

Reputation: 7466

myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

is correct.

If it doesn't work, then the assumption that there is a combination of "\" and "n" characters is wrong.

Do not use NSLog. NSLog already applies carriage returns to the string. Instead put a breakpoint on the line where we call stringByReplacing... and then hover over the myString. Wait a second or two and you will see the "original unformatted content"...this way you can check what you are really trying to replace..

Upvotes: 2

Related Questions