2012-08-16 66 views
1

我在那裏我試圖刪除訪問空間,括號和數組」刪除空格,括號和從NSArray中才能使用componentsSeparatedByString:@";"如何「從NSArray中

NSArray *paths = [dic valueForKey:@"PATH"]; 

    for(NSString *s in paths) 
    { 
     NSLog(@"String: %@", s); 
    } 

String: (
"29858,39812;29856,39812;29800,39819;29668,39843;29650,39847;29613,39855;29613,39855;29613,39856;29605,39857;29603,39867;29603,39867;29599,39892;29596,39909;29587,39957;29571,40018;29563,40038;29560,40043" 
) 

這是輸出給定如顯示有空格,括號和「我怎麼可以刪除它們 ? 由於這條線juz一個字符串內的數組"29858,39812;29856,39812;29800,39819;29668,39843;29650,39847;29613,39855;29613,‌​39855;29613,39856;29605,39857;29603,39867;29603,39867;29599,39892;29596,39909;295‌​87,39957;29571,40018;29563,40038;29560,40043" this line is a string inside the path array,我嘗試使用componentsSeparatedByString:@";"它不能溢出所有有空格括號和」裏面。

回答

3

嘗試stringByTrimmingCharactersInSet:

NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"() \n\""]; 
s = [s stringByTrimmingCharactersInSet:charsToTrim]; 
+1

我使用for循環中的代碼,但它有一個錯誤。 ' - [__ NSArrayM stringByTrimmingCharactersInSet:]:無法識別的選擇器發送到實例0x9a8bc0' – sihao 2012-08-16 11:51:45

+0

@ user1495988這意味着您的* s包含數組。不是字符串 – janusbalatbat 2012-08-16 11:54:23

+0

那麼,它不是你的數組中的字符串,而是另一個數組。在你的循環中試試這個:'NSLog(NSStringFromClass([s class]));' – DrummerB 2012-08-16 11:54:55

0

嘗試使用:

s = [s stringByReplacingOccurrencesOfString:@";" 
            withString:@""]; 
0

爲你分開的數字,你可以與他們即NSInteger值工作。

NSString *_inputString = @"29858,39812;29856,39812;29800,39819;29668,39843;29650,39847;29613,39855;29613,39855;29613,39856;29605,39857;29603,39867;29603,39867;29599,39892;29596,39909;29587,39957;29571,40018;29563,40038;29560,40043"; 
NSString *_setCommonSeparator = [_inputString stringByReplacingOccurrencesOfString:@";" withString:@","]; 
NSArray *_separetedNumbers = [_setCommonSeparator componentsSeparatedByString:@","]; 

for (NSString *_currentNumber in _separetedNumbers) { 
    NSInteger _integer = [_currentNumber integerValue]; 
    NSLog(@"number : %d", _integer); 
}