NSString 和NSCharacterset的不同之處
An NSString object represents a string of ordered characters (text). An NSCharacterSet object represents a set of characters in no particular order. It is often much quicker to determine whether a character is a member of an NSCharacterSet than an NSString. You can’t use an NSCharacterSet object to hold text, at least not in any sensible way, because it does not maintain any order of characters, its use is primarily for determining whether a character exists in a set of characters.
我們?cè)趎sstring的分割齐莲,查找等操作中拨匆,經(jīng)常會(huì)提供兩種函數(shù)妓蛮,參數(shù)類型分別為NSString 和NSCharacterset,有什么不同呢舷蒲?
NSString 是有序字符串
NSCharacterset是無(wú)需字符集合,主要用來(lái)判斷已知字符串是否包含制定字符集系洛,而不可以用來(lái)保存字符串而晒。
轉(zhuǎn)自:http://blog.csdn.net/aiyongyyy/article/details/8269546
在ios中 ??可以使用stringByTrimmingCharactersInSet函數(shù)過(guò)濾字符串中的特殊符號(hào)
首先自己定義一個(gè)NSCharacterSet, 包含需要去除的特殊符號(hào)
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"/@/:碎绎;()¥「」/"螃壤、[]{}#%-*+=_\\|~<>$€^?’@#$%^&*()_+’\""];
由于NSString中有全角符號(hào)和半角符號(hào), 因此有些符號(hào)要包括全角和半角的
然后調(diào)用stringByTrimmingCharactersInSet
NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];
trimmedString就是過(guò)濾后的字符串
轉(zhuǎn)自:http://www.gelubpa.com/2012/8/10/00702.html
NSCharacterSet
實(shí)例應(yīng)用:
一、過(guò)濾字符串開始位置和結(jié)束位置的空格
NSString *testString = @"my name is wang tianqiao ";
NSString *cString = [testString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"cString is %@",cString);
轉(zhuǎn)自:http://blog.sina.com.cn/s/blog_5421851501014xif.html
NSCharacterSet 去除NSString中的空格
去除 username中的空格筋帖,tablenewline奸晴,nextline
代碼如下:(三行代碼)
NSCharacterSet *whitespace =[NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *username = [mUsernameField stringValue];
username =[username stringByTrimmingCharactersInSet:whitespace];
注釋:
stringByTrimmingCharactersInSet:
Returns a new string made by removing from both ends of the receiver 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).
NSString *temptext = [messageTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *text = [temptext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
第1行是去除2端的空格
第2行是去除回車
轉(zhuǎn)自:http://mikixiyou.iteye.com/blog/1623323
如何去掉String的前后空格或某字符
在objective-c中,如何去掉一個(gè)string的前后的空格字符或某個(gè)特定字符呢日麸?
如@“ abc 123 ”字符串前后有空格寄啼,該如何去掉?
使用nsstring的實(shí)例方法stringByTrimmingCharactersInSet:可以解決該問(wèn)題赘淮。
方法如下:
OC代碼
[@"abc123" stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]辕录;
NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString中該方法說(shuō)明如下:
stringByTrimmingCharactersInSet:
Returns a new string made by removing from both endsof the receiver characters contained in a given character set.
– (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet*)set
Parameters
set
A character set containing the characters to removefrom the receiver. set must not be nil.
Return Value
A new string made by removing from both ends of thereceiver characters contained in set. If the receiver is composedentirely of characters from set, the empty string is returned.