美文网首页iOS 基础知识iOS Developer
OC中字符串中去除特殊符号 stringByTrimmingCh

OC中字符串中去除特殊符号 stringByTrimmingCh

作者: 走在路上的小二 | 来源:发表于2016-11-24 10:18 被阅读272次

stringByTrimmingCharactersInSet:

注释:Returns a new string made by removing from both ends of thereceiver characters contained in a given characterset.

whitespaceAndNewlineCharacterSet

注释:Returns a character set containing only the whitespace charactersspace (U+0020) and tab (U+0009) and the newline and nextlinecharacters (U+000A–U+000D, U+0085).

另外可以用whitespaceCharacterSet替换whitespaceAndNewlineCharacterSet区别newlinenextline

whitespaceCharacterSet

注释:Returns a character set containing only the in-line whitespacecharacters space (U+0020) and tab (U+0009).

// 字符串除去空格后 字符串的个数

NSString *temptext = [messageTextField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];

NSString *text = [temptextstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet ]];

注释:第1行是去除2端的空格  ,第2行是去除回车

// 限制输出空格

NSCharacterSet *whitespaceSet = [NSCharacterSetwhitespaceCharacterSet];

if([currentStringrangeOfCharacterFromSet:whitespaceSet].location != NSNotFound){

returnNO;

}

PS: 希望小二提供的有所帮助,欢迎留言。

相关文章

网友评论

    本文标题:OC中字符串中去除特殊符号 stringByTrimmingCh

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