how to remove spaces, brackets and " from nsarray

亡梦爱人 提交于 2019-12-24 06:44:49

问题


I have an array where i am trying to remove the access spaces, brackets and " from the nsarray in order to use componentsSeparatedByString:@";"

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"
)

this is the output give as show there are spaces, brackets and " how could i remove them ? As this line is juz a string inside that array "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 and i try using componentsSeparatedByString:@";" it could not be spilt all there are spaces brackets and " inside.


回答1:


Try stringByTrimmingCharactersInSet:

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



回答2:


try to use:

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



回答3:


it separates the numbers for you and you can work with them as i.e. NSInteger values.

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);
}


来源:https://stackoverflow.com/questions/11986337/how-to-remove-spaces-brackets-and-from-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!