替换连续的空格,制表符,换行符

时间:2012-04-22 13:33:26

标签: iphone ios nsstring

我有一个带有连续空格,制表符,换行符的NSString。有没有方法只用一个空间重播它们?我有一个小的UIlabel视图来显示它们。

1 个答案:

答案 0 :(得分:4)

你可以试试这个:

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

这里的关键是来自Apple doc的whitespaceAndNewlineCharacterSet:

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