0

我有一个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"];
}
4

5 回答 5

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

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

于 2012-08-16T06:35:58.063 回答
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:@";"];

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

于 2012-08-16T06:34:35.790 回答
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
}
于 2012-08-16T07:25:33.057 回答
0

我这样做是为了替换 .plist 中的字符串,所以它可能对你有用

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

        [array1 writeToFile:Path1 atomically:YES];
    NSLog(@"count: %@", [array1 objectAtIndex:1]);
于 2012-08-16T06:57:58.177 回答
0

//将数组复制到字符串中

NSString *str = [路径 componentsJoinedByString:@""];

//然后替换“|”

str = [str stringByReplacingOccurrencesOfString:@"|" withString:@";"];

于 2012-08-16T06:37:54.870 回答