替换nsmutablearray中的对象

时间:2012-08-16 06:26:39

标签: objective-c ios nsmutablearray xcode4.3

我有一个NSMutableArray我希望将符号|替换为;我该怎么做?

NSMutableArray *paths = [dic valueForKey:@"PATH"];
NSLog(@"pathArr ",  paths)
pathArr (
    (
       "29858,39812;29858,39812;29925,39804;29936,39803;29949,39802;29961,39801;30146,39782;30173,39779;30220,39774;30222,39774|30215,39775;30173,39779;30146,39782;29961,39801;29949,39802;29936,39803;29925,39804;29858,39812;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"
    )
)

更新

这是我从

获取路径的地方
NSArray *BusRoute = alightDesc;
int i;
int count = [BusRoute count];
for (i = 0; i < count; i++)
{   
    NSLog (@"BusRoute = %@", [BusRoute objectAtIndex: i]);
    NSDictionary *dic = [BusRoute objectAtIndex: i];
    NSMutableArray *paths = [dic valueForKey:@"PATH"];
}

5 个答案:

答案 0 :(得分:2)

假设您的数组路径中的对象是字符串,您可以执行此操作

NSMutableArray *path2=[[NSMutableArray alloc]initWithArray:nil];
for (NSObject *obect in path) {
    for (NSString *string in (NSArray*)obect) {
        [path2 addObject:[string stringByReplacingOccurrencesOfString:@"|" withString:@","]];
    }

}

NSLog(@"pathArr %@ ",  path2);

您的数组路径包含另一个以字符串作为对象的数组。 希望这有帮助

答案 1 :(得分:0)

您可以将路径转换或转换为NSString,然后执行:

paths  = (NSString *) [paths stringByReplacingOccurrencesOfString:@"|" withString:@";"];

如果这不起作用,请创建包含pathArr文本的新NSString实例,调用replaceOccurrences方法并执行反转转换

 NSMutableString *tempStr = [[NSMutableString alloc] init];
 for (int i = 0; i < [paths count]; i++) 
{  
   [tempStr appendString:[path objectAtIndex:i]];
}

然后将此方法用于tempStr。然后尝试:

NSArray *newPaths = [tempStr componentsSeparatedByString:@";"];

可能是最后一种方法并不完全正确,因此请尝试使用它。

答案 2 :(得分:0)

//将数组复制到字符串

NSString * str = [paths componentsJoinedByString:@“”];

//然后替换“|”

str = [str stringByReplacingOccurrencesOfString:@“|” withString:@ “;”];

答案 3 :(得分:0)

我这样做是为了替换.plist中的字符串,这样它可能适合你

    array1 = [NSMutableArray arrayWithContentsOfFile:Path1];
NSString *item = [@"dfdfDF"];
[array1 replaceObjectAtIndex:1 withObject:item];

        [array1 writeToFile:Path1 atomically:YES];
    NSLog(@"count: %@", [array1 objectAtIndex:1]);

答案 4 :(得分:0)

呃,你为什么不去:

NSString *cleanedString = [[[dic valueForKey:@"PATH"] objectAtIndex:0] stringByReplacingOccurrencesOfString:@";" withString:@"|"];

如果有多个嵌套数组,可以去

for(int i = 0; i < [[dic valueForKey:@"PATH"] count]; i++)
{
    NSString *cleanedString = [[[dic valueForKey:@"PATH"] objectAtIndex:i] stringByReplacingOccurrencesOfString:@";" withString:@"|"];

    // do something with cleanedString
}
相关问题