Object-C去掉字符串空格和n符号等
在Objective-C中,可以使用NSString的stringByReplacingOccurrencesOfString:withString:方法来去掉字符串中的空格和换行符等特殊字符。
下面是一个示例代码:
NSString *originalString = @" This is a test string.\n";
NSString *trimmedString = [originalString stringByReplacingOccurrencesOfString:@" " withString:@""];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSLog(@"%@", trimmedString);
输出结果为:
Thisisateststring.
该方法会返回一个新的字符串,不会修改原始字符串。你可以使用这个方法来去掉字符串中的空格和换行符等特殊字符。
原文地址: https://www.cveoy.top/t/topic/iKst 著作权归作者所有。请勿转载和采集!