Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

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

My NSString is like this:

NSString *myString =  
(  
“
 
 24 K CLUB”,  
“
 
 3 DOLLAR CAFE”,  
“
 
 A PEACH OF A PARTY”,  
“
 
 A ROYAL AFFAIR CAFE”,  
“
 
 AFFAIRS TO REMEMBER CATERERS”,  
“
 
 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:@"
" withString:@""];  
myString = [myString stringByReplacingstringByReplacingOccurrencesOfString:@" " withString:@""];

but unsuccessfully..getting error:

[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x7062200
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...