NSString 處理技巧:分割字串

來源:互聯網
上載者:User

標籤:

 摘要 string類型是objective-c中用的最多的類型之一,有時會出現字串中有我們不想要的字元。 如 "hello world"中的空格,或是"hello/world"中的‘/‘,亦或是"你好A你好"中的‘A‘。這些都可以通過NSString中的方法來解決。 

一、帶節點的字串,如@"<p>討厭的節點<br/></p>"我們只想要中間的中文

處理方法一:

 

NSString *string1 = @"<p>討厭的節點<br/></p>";                /*此處將不想要的字元全部放進characterSet1中,不需另外加逗號或空格之類的,除非字串中有你想要去除的空格,此處< p /等都是單獨存在,不作為整個字元*/                NSCharacterSet *characterSet1 = [NSCharacterSet characterSetWithCharactersInString:@"<p/brh>"];                // 將string1按characterSet1中的元素分割成數組        NSArray *array1 = [string1 componentsSeparatedByCharactersInSet:characterSet1];                NSLog(@"array = %@",array1);                for(NSString *string1 in array1)        {            if ([string1 length]>0) {                                // 此處string即為中文字串                NSLog(@"string = %@",string1);            }        }

列印結果: 2013-05-31 10:55:34.017 string[17634:303] 

array = (
    "",
    "",
    "",
    "\U8ba8\U538c\U7684\U8282\U70b9",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
)
2013-05-31 10:55:34.049 string[17634:303] 
string = 討厭的節點

 

二、帶空格的字串,如

@"hello world"去掉空格

 

NSString *string2 = @"hello world";                /*處理空格*/                NSCharacterSet *characterSet2 = [NSCharacterSet whitespaceCharacterSet];                // 將string1按characterSet1中的元素分割成數組        NSArray *array2 = [string2 componentsSeparatedByCharactersInSet:characterSet2];                NSLog(@"\narray = %@",array2);                // 用來存放處理後的字串        NSMutableString *newString1 = [NSMutableString string];                for(NSString *string in array1)        {            [newString1 appendString:string];        }        NSLog(@"newString = %@", newString1);

列印結果:

2013-05-31 11:02:49.656 string[17889:303] 
array = (
    hello,
    world
)
2013-05-31 11:02:49.657 string[17889:303] newString = helloworld

PS:處理字母等其他元素只需將NSCharacterSet的值改變即可。

 

 

+ (id)controlCharacterSet;+ (id)whitespaceCharacterSet;+ (id)whitespaceAndNewlineCharacterSet;+ (id)decimalDigitCharacterSet;+ (id)letterCharacterSet;+ (id)lowercaseLetterCharacterSet;+ (id)uppercaseLetterCharacterSet;+ (id)nonBaseCharacterSet;+ (id)alphanumericCharacterSet;+ (id)decomposableCharacterSet;+ (id)illegalCharacterSet;+ (id)punctuationCharacterSet;+ (id)capitalizedLetterCharacterSet;+ (id)symbolCharacterSet;+ (id)newlineCharacterSet NS_AVAILABLE(10_5, 2_0);+ (id)characterSetWithRange:(NSRange)aRange;+ (id)characterSetWithCharactersInString:(NSString *)aString;+ (id)characterSetWithBitmapRepresentation:(NSData *)data;+ (id)characterSetWithContentsOfFile:(NSString *)fName;

NSString 處理技巧:分割字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.