如何从NSString中删除标题和尾随空格 [英] how to get rid of heading and trailing white spaces from NSString

查看:92
本文介绍了如何从NSString中删除标题和尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的NSString是这样的:

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"  )

如何摆脱这个新行字符和空格,以便我的新字符串如下:
newString:

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" 
) 

我试过:

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

但未成功..出现错误:

but unsuccessfully..getting error:

[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x7062200


推荐答案

怎么样 stringByTrimmingCharactersInSet:方法?
通过 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] ,它可以删除空格和换行符的两端。

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

这篇关于如何从NSString中删除标题和尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆