美文网首页程序员
【code_小马】字符串NSString中去掉空格(或替换为某个

【code_小马】字符串NSString中去掉空格(或替换为某个

作者: 曾经像素有点低 | 来源:发表于2016-12-21 19:14 被阅读31125次
我们是工程师 ——加油 !

(1)系统去首尾空格方法,使用NSString中的str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]方法只是去掉左右两边的空格;

(2)替换的方法,使用NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];可以去掉空格,注意此时生成的strUrl是autorelease属性的,不要妄想对strUrl进行release操作。


//对比

NSString *temptext = [messageTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSString *text = [temptext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];

第1行是去除两端的空格

第2行是去除回车

【code_小马】更多iOS实用战术

相关文章

网友评论

    本文标题:【code_小马】字符串NSString中去掉空格(或替换为某个

    本文链接:https://www.haomeiwen.com/subject/zsoqvttx.html