NSString 分割
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.
我們在nsstring的分割,查找等操作中,經常會提供兩種函數,參數類型分別為NSString 和NSCharacterset,有什么不同呢?
NSString 是有序字符串
NSCharacterset是無需字符集合,主要用來判斷已知字符串是否包含制定字符集,而不可以用來保存字符串。
轉自:http://blog.csdn.net/shencaifeixia1/article/details/8232593
在ios中 可以使用stringByTrimmingCharactersInSet函數過濾字符串中的特殊符號
首先自己定義一個NSCharacterSet, 包含需要去除的特殊符號
NSCharacterSet *set = [NSCharacterSetcharacterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^·'@#$%^&*()_+'""];
由於NSString中有全角符號和半角符號, 因此有些符號要包括全角和半角的
然后調用stringByTrimmingCharactersInSet
NSString *trimmedString = [stringstringByTrimmingCharactersInSet:set];
trimmedString就是過濾后的字符串
轉自:http://blog.csdn.net/aiyongyyy/article/details/8269546
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 區別newlinenextline
whitespaceCharacterSet
Returns a character set containing only the in-line whitespacecharacters space (U+0020) and tab (U+0009).
NSString *temptext =[messageTextField.texts tringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
NSString *text = [temptextstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet ]];
第1行是去除2端的空格
第2行是去除回車
轉自:http://blog.sina.com.cn/s/blog_5421851501014xif.html
這是典型的其他語言中trim方法。我要問的是,如何去掉最左邊的空格?又該如何去掉最右邊的空格?
在NSString的類中沒有提供實現這類需求的方法,我們只能手工去添加這些方法。
@interface NSString (TrimmingAdditions)
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet ;
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet ;
@end
@implementation NSString (TrimmingAdditions)
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {
NSUInteger location = 0;
NSUInteger length = [self length];
unichar charBuffer[length];
[self getCharacters:charBuffer];
for (location; location < length; location++) {
if (![characterSet characterIsMember:charBuffer[location]]) {
break;
}
}
return [self substringWithRange:NSMakeRange(location, length - location)];
}
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet {
NSUInteger location = 0;
NSUInteger length = [self length];
unichar charBuffer[length];
[self getCharacters:charBuffer];
for (length; length > 0; length--) {
if (![characterSet characterIsMember:charBuffer[length - 1]]) {
break;
}
}
return [self substringWithRange:NSMakeRange(location, length - location)];
}
@end
使用stringByTrimmingRightCharactersInSet方法去掉一個字符串最右邊的空格。
NSLog(@"%@",[@"abc 123 " stringByTrimmingRightCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);
如何去掉一個字符串@“0.012300”最右邊的“0“呢?這是典型的float格式化成字符串以后的形式,顯示時最好去掉最右邊的“0”。
解決方法如下:
[@"0.012300" stringByTrimmingRightCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]];
在NSString中trim字符時,不理解為何不提供分別去掉左右字符的方法,如其他語言的ltrim和rtim。