views:

108

answers:

1

Hi,

I have a string that ends with a " (quotation mark) that I want to get rid of. However, because XCode usually requires you to enter the text you wish to remove using stringByReplacingOccurrencesOfString in @"texttoremove" format, you can't use the quotation marks in the space as it thinks you are closing the text.

Update: Now I can't get rid of "\n\t\t that's at the end of each string. Any ideas?

Any ideas on how I can do it?

Thanks.

+3  A: 

To enter a quote in an Objective-C string you just have to escape it with a '\'. So a string containing just a quote is:

@"\""

Update: Within a string literal, "\n" and "\t" are escape sequences identifying the newline and tab characters respectively. I assume you mean the string ends with a newline & 2 tabs, not literal '\' and 'n' characters....

You probably want to use stringByTrimmingCharactersInSet:, for example:

NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

(Note that this will trim whitespace at both ends)

David Gelhar
Thanks! Now have another issue though, I can't get rid of "\n\t\t in my string, I think because of the \. Any ideas? Have added an update above.
Graeme
Actually I did mean literal \ and 'n' characters- I probably should have made that more clear. Is there any other way to remove them? Thanks.
Graeme
To refer to a literal '\' inside a string, you need to escape the backslash by doubling it: @"\\".
David Gelhar
Thanks. It worked. I'm still working out all these things! :)
Graeme