iOS筆記之NSString基礎(chǔ)類(Objective-C)

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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市乖坠,隨后出現(xiàn)的幾起案子搀突,更是在濱河造成了極大的恐慌,老刑警劉巖熊泵,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仰迁,死亡現(xiàn)場離奇詭異甸昏,居然都是意外死亡,警方通過查閱死者的電腦和手機徐许,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門施蜜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人雌隅,你說我怎么就攤上這事翻默。” “怎么了澄步?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵冰蘑,是天一觀的道長。 經(jīng)常有香客問我村缸,道長,這世上最難降的妖魔是什么武氓? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任梯皿,我火速辦了婚禮,結(jié)果婚禮上县恕,老公的妹妹穿的比我還像新娘东羹。我一直安慰自己,他們只是感情好忠烛,可當(dāng)我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布属提。 她就那樣靜靜地躺著,像睡著了一般美尸。 火紅的嫁衣襯著肌膚如雪冤议。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天师坎,我揣著相機與錄音恕酸,去河邊找鬼。 笑死胯陋,一個胖子當(dāng)著我的面吹牛蕊温,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播遏乔,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼义矛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了盟萨?” 一聲冷哼從身側(cè)響起凉翻,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鸯旁,沒想到半個月后噪矛,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體量蕊,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年艇挨,在試婚紗的時候發(fā)現(xiàn)自己被綠了残炮。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡缩滨,死狀恐怖势就,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情脉漏,我是刑警寧澤苞冯,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站侧巨,受9級特大地震影響舅锄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜司忱,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一皇忿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧坦仍,春花似錦鳍烁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至梳玫,卻和暖如春爹梁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背汽纠。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工卫键, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人虱朵。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓莉炉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親碴犬。 傳聞我的和親對象是個殘疾皇子絮宁,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 第2章 基本語法 2.1 概述 基本句法和變量 語句 JavaScript程序的執(zhí)行單位為行(line),也就是一...
    悟名先生閱讀 4,153評論 0 13
  • 一服协、Java 簡介 Java是由Sun Microsystems公司于1995年5月推出的Java面向?qū)ο蟪绦蛟O(shè)計...
    子非魚_t_閱讀 4,206評論 1 44
  • 今天開始練習(xí)專注的讀書绍昂、專注的想一件事兒。其實這本來也是我擅長的,只是手機毀人窘游,我覺得我有點兒「攜帯依存癥」總看...
    貓公主喵閱讀 253評論 0 0
  • Given a string containing just the characters '(', ')', '...
    a_void閱讀 192評論 0 0
  • 時間日期類:NSDate 時間的格式符:注意大小寫年 year ->y月 ...
    J_coco閱讀 877評論 0 2