个stringByReplacingOccurrencesOfS

个stringByReplacingOccurrencesOfS

This question already has answers here:
Remove all whitespaces from NSString
                                
                                    (11个答案)
                                
                        
                                6年前关闭。
            
                    
我可以用更少的代码来做到这一点吗?

urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];
urlString = [urlString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
urlString = [urlString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
urlString = [urlString stringByReplacingOccurrencesOfString:@"\r" withString:@""];

最佳答案

如果您只想从字符串的开头和结尾处修剪空白和空白行,则可以使用

urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


这包括您提供的所有示例。

关于ios - 多个stringByReplacingOccurrencesOfString的有效方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20671054/

10-10 20:36