IOS NSCharacterSet 去除NSString中的空格

去除 username中的空格,table newline,nextline 
代码如下:

NSCharacterSet *whitespace = [NSCharacterSet  whitespaceAndNewlineCharacterSet];

NSString * string [email protected]"liujun        \t          ";


string = [string  stringByTrimmingCharactersInSet:whitespace]; 

注释

stringByTrimmingCharactersInSet
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

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

另外可以用 whitespaceCharacterSet 替换 whitespaceAndNewlineCharacterSet 区别newline nextline 
whitespaceCharacterSet 
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).

NSString *content = @"    kahdahdah       \t           ";

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

第1行是去除2端的空格

第2行是去除回车

NSCharacterSet的各个枚举类型的含义如下:

 1 controlCharacterSet//控制符
 2 whitespaceCharacterSet
 3 whitespaceAndNewlineCharacterSet//空格换行
 4 decimalDigitCharacterSet//小数
 5 letterCharacterSet//文字
 6 lowercaseLetterCharacterSet//小写字母
 7 uppercaseLetterCharacterSet//大写字母
 8 nonBaseCharacterSet//非基础
 9 alphanumericCharacterSet//字母数字
10 decomposableCharacterSet//可分解
11 illegalCharacterSet//非法
12 punctuationCharacterSet//标点
13 capitalizedLetterCharacterSet//大写
14 symbolCharacterSet//符号
15 newlineCharacterSet//换行符
时间: 2024-08-29 22:10:41

IOS NSCharacterSet 去除NSString中的空格的相关文章

NSCharacterSet去除字符串中的空格、删除指定\任意字符集

一.去除首尾的空格 /** 1.去除首尾的空格*/ NSString *strMsg=@" 简书作者:CoderZb "; NSString *strResult = [strMsg stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"去除空格前:%@",strMsg); NSLog(@"去除空格后:%@",strResult); 去除

去除字符串中的空格

用C语言写一个函数,去除字符串中的空格,并返回删除的空格的个数.不允许开辟新的空间,只能申请简单类型的自动变量.时间复杂度要求为O(n). #include <bitset> #include<iostream> int deleteSpace(char * pstr); void main() { char word[]="dhkak df d fd fdjfkda dfd ff f fd da "; deleteSpace(word); std::cout&

用较小的代价去除字符串中的空格

题目: 用C语言写一个函数,去除字符串中的空格,并返回删除的空格的个数.不允许开辟新的空间,只能申请简单类型的自动变量.时间复杂度要求为O(n). 比如:char str[]="dhkak   df d fd     fdjfkda     dfd   ff f  fd da "; 处理之后: str[]="dhkakdfdfdfdjfkdadfdffffdda"; 返回删除空格的个数为:12算法思想: 先取字符串的长度,然后用前后各一个指针,分别为p,q,使前面一

php如何清除html格式并去除文字中的空格然后截取文字

PHP如何清除html格式并去除文字中的空格然后截取文字,详细分享一下处理方法(顺便对PHP清除HTML字符串的函数做了一个小结): htmlspecialchars 将特殊字元转成 HTML格式语法: string htmlspecialchars(string string);传回值: 字串函式种类: 资料处理内容说明 本函式将特殊字元转成 HTML 的字串格式 ( &....; ).最常用到的场合可能就是处理客户留言的留言版了.& (和) 转成 & " (双引号)

Java 去除字符串中的空格和其他字符

直接上代码了. <span style="font-size:18px;">import java.util.regex.Matcher; import java.util.regex.Pattern; /** * java 去除字符串中的空格和其他字符 * @author YYBJ * @date 2014-10-19 */ public class CleanString { public static String replaceBlank(String str) {

java 去html标签,去除字符串中的空格,回车,换行符,制表符

public static String getonerow(String allLine,String myfind)     {                           Pattern pattern = Pattern.compile("<div class=\"row\">.*?</div>");                      Matcher  matcher = pattern.matcher(allLine

修改文件名并去除文件名中的空格

1 //修改文件名 2 private static void ReviseFileName(string[] files) 3 { 4 for (int i = 0; i < files.Length; i++) 5 { 6 string file = files[i]; 7 string newFile = RemoveBlank(file); 8 File.Move(file, newFile); 9 } 10 } 11 //去除字符串中的空格 12 private static stri

Linux shell去除字符串中所有空格

Linux shell去除字符串中所有空格 echo $VAR | sed 's/ //g' 原文地址:https://www.cnblogs.com/yjd_hycf_space/p/9839494.html

iOS字符串NSString中去掉空格(或替换为某个字符串)

http://blog.sina.com.cn/s/blog_6f29e81f0101qwbk.html [问题描述] 今天请求服务器返回的字段中含有空格,这空格是服务器开发人员不小心往数据中多敲了,所以在客户端需要把字符串中的空格过滤掉. [问题分析] 1.使用NSString中的stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]方法只是去掉左右两边的空格: 2.使用NSString *strUrl