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: 希望小二提供的有所帮助,欢迎留言。
网友评论