2018.4.3
文中諸多內(nèi)容屬直接轉(zhuǎn)載鼻种,若原作者要求刪除,請聯(lián)系我泛领,立刪一喘。
1. 通過stringByTrimmingCharactersInSet去除字符串兩端的特殊符號
原作者:BlackWolfSky
原文:通過stringByTrimmingCharactersInSet去除字符串兩端的特殊符號
1酝豪、函數(shù)簡介
蘋果的NSString類提供了stringByTrimmingCharactersInSet方法過濾字符串兩端的特殊符號涛碑,函數(shù)聲明如下:
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
對于該函數(shù)蘋果官方的說明為:Returns a new string made by removing from both ends of the receiver characters contained in a given character set. 通過官方說明我們知道該函數(shù)將字符串兩端的與set集合中的成員匹配的字符過濾掉。
2寓调、函數(shù)使用
該函數(shù)的入?yún)⑹荖SCharacterSet類型锌唾,所以使用該函數(shù)之前需要定義一個NSCharacterSet變量,當(dāng)然也可以使用系統(tǒng)提供的NSCharacterSet常量夺英,如whitespaceAndNewlineCharacterSet晌涕、whitespaceCharacterSet等
3、舉例如下:
a痛悯、自定義一個NSCharacterSet, 包含需要去除的特殊符號
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"余黎。。载萌。"];
NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];
trimmedString是過濾后的字符串
b惧财、使用系統(tǒng)的集合
NSString *text1 = [TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceCharacterSet]];//除兩端的空格
NSString *text = [TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去除兩段的空格和回車注釋:
注釋:
1、whitespaceAndNewlineCharacterSet
Returns a character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
2扭仁、whitespaceCharacterSet
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
2.關(guān)于刪去字符串中不想要的字符
a.如果只是需要簡單刪掉
空格
或者回車
NSString *string = @"Test string, please enter it";
/* 去掉字符串中的空格和換行 */
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
/* 或者 */
NSMutableString *mutStr = [NSMutableString stringWithString:string];
NSRange range = {0, string.length};
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
range = {0, mutStr.length};
[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range];
string = [mutStr copy];
b.刪去稍復(fù)雜一些的字符
NSString *string = @"Test string, please enter it";
/* 過濾不想要的(非法)字符垮衷,filterString自定義 */
NSString *filterString = @"[]{}(#%-*+=_)\\|~(<>$%^&*)_+ ";
filterString = @" \n"; /* 示例:僅處理空格和換行 */
NSCharacterSet *filterSet = [NSCharacterSet characterSetWithCharactersInString:filterString];
string = [[string componentsSeparatedByCharactersInSet:filterSet] componentsJoinedByString:@""];
3.字符串替換的姿勢
姿勢1 - 針對多語言版本
/* 請將智能設(shè)備靠近手機并保持開機狀態(tài)
Please keep the smart device close to the phone and keep it turned on
"請將智能設(shè)備%@手機\n并保持%@狀態(tài)" = "Please keep the smart device %@ the phone and keep it %@";
"靠近" = "close to";
"開機" = "turned on";
*/
NSString *specialString = @"<*#*>";
NSString *initString = [NSString stringWithFormat:@"請將智能設(shè)備%@手機\n并保持%@狀態(tài)", specialString, specialString];
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
NSForegroundColorAttributeName:[UIColor lightGrayColor]};
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]
initWithString:initString attributes:attributes];
NSRange range1 = [attributedText.string rangeOfString:specialString];
attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
NSForegroundColorAttributeName:[UIColor blackColor]};
[attributedText replaceCharactersInRange:range1
withAttributedString:[[NSAttributedString alloc]
initWithString:@"靠近" attributes:attributes]];
NSRange range2 = [attributedText.string rangeOfString:specialString];
attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
NSForegroundColorAttributeName:[UIColor blackColor]};
[attributedText replaceCharactersInRange:range2
withAttributedString:[[NSAttributedString alloc]
initWithString:@"開機" attributes:attributes]];
NSString *resultString = attributedText.string;