iOS 基础知识iOS Developer

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

2016-11-24  本文已影响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: 希望小二提供的有所帮助,欢迎留言。

上一篇下一篇

猜你喜欢

热点阅读