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