Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get rid of heading and trailing white spaces from NSString

Tags:

iphone

My NSString is like this:

NSString *myString =  
(  
“\n \n 24 K CLUB”,  
“\n \n 3 DOLLAR CAFE”,  
“\n \n A PEACH OF A PARTY”,  
“\n \n A ROYAL AFFAIR CAFE”,  
“\n \n AFFAIRS TO REMEMBER CATERERS”,  
“\n \n AFRIKAN DELI”  )

How to get rid of this new line character and white spaces, so that my new string will be like: newString:

(
"24 K CLUB”,  
"3 DOLLAR CAFE”,  
“A PEACH OF A PARTY”,  
“A ROYAL AFFAIR CAFE”,  
“AFFAIRS TO REMEMBER CATERERS”,  
“AFRIKAN DELI” 
) 

I tried :

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

but unsuccessfully..getting error:

[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x7062200
like image 908
AKG Avatar asked Sep 26 '10 23:09

AKG


People also ask

How do you remove leading and trailing space?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

How do I remove the white spaces from a string in Objective C?

Use whitespaceCharacterSet or whitespaceAndNewlineCharacterSet to remove whitespace around strings.

Which function used to remove all leading and trailing whitespace?

The Trim function The most obvious (and generally efficient) method for removing both leading and trailing space is to use the TRIM() function.

What does trailing white space mean?

Trailing whitespace is any spaces or tabs after the last non-whitespace character on the line until the newline.


1 Answers

How about stringByTrimmingCharactersInSet: method? By stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet], it can remove both ends of whitespace and newline characters.

like image 112
AechoLiu Avatar answered Oct 15 '22 08:10

AechoLiu