Search code examples
iphoneobjective-cnsstringnslog

NSString stringWithFormat adding a newline by itself


Why does this:

NSString *url = [NSString stringWithFormat:@"http://www.example.com/profile/%@/?s_iphone=true", author];
NSLog(@"url: %@", url);

Output this:

http://www.example.com/profile/AuthorName
            /?s_iphone=true

Needless to say the url won't load b/c there is a newline added to the string by itself. I've tried removing whitesapces/newlines and still had the same output. Its driving me crazy.

Matt


Solution

  • Try this -

    NSString *url = [NSString stringWithFormat:@"http://www.example.com/profile/%@/?s_iphone=true", [author stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSLog(@"url: %@", url);
    

    It will fix any new line characters at the beginning and end of your author string.