2012-04-22 61 views

回答

4

你可以用这个尝试:

NSString *string = @" spaces in front and at the end "; 
NSString *trimmedString = [string stringByTrimmingCharactersInSet: 
           [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

的这里关键是whitespaceAndNewlineCharacterSet,从Apple doc

Returns a character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085). 
+0

是的,我知道这种方法的一个例子。但我想修剪我们的字符串中间的空白,就像“中间的空间” – 2012-04-22 13:50:02

+0

它的作品,谢谢。 – 2015-01-15 11:35:46