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區(qū)別newlinenextline
whitespaceCharacterSet
注釋:Returns a character set containing only the in-line whitespacecharacters space (U+0020) and tab (U+0009).
// 字符串除去空格后 字符串的個數(shù)
NSString *temptext = [messageTextField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
NSString *text = [temptextstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet ]];
注釋:第1行是去除2端的空格? ,第2行是去除回車
// 限制輸出空格
NSCharacterSet *whitespaceSet = [NSCharacterSetwhitespaceCharacterSet];
if([currentStringrangeOfCharacterFromSet:whitespaceSet].location != NSNotFound){
returnNO;
}
PS: 希望小二提供的有所幫助胚嘲,歡迎留言轿秧。