0

I have a NSMutableArray where i want to replace the sign | into a ; how can i do that?

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

Update

This is where i got my path from

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"];
}
1
  • There's only one object inside your array, and it's a string. Your problem is removing these characters from the string, not objects from an array. Commented Aug 16, 2012 at 18:07

5 Answers 5

2

Provide that your object in the array path is string, you can do this

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

your array paths contains an another array which has string as object. Hope this helps

Sign up to request clarification or add additional context in comments.

8 Comments

got an error for it -[__NSArrayM stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x91ccc0
can you paste your code? are you sure you called that function using string object?
wat do u mean? [path2 addObject:[string stringByReplacingOccurrencesOfString:@"|" withString:@","]]; this line got the error
for (NSString *string in paths) did you used this condition
yup i used NSMutableArray *path2=[[NSMutableArray alloc]initWithArray:nil]; for (NSString *string in paths) { [path2 addObject:[string stringByReplacingOccurrencesOfString:@"|" withString:@";"]]; }
|
0

//Copy the Array into a String

NSString *str = [paths componentsJoinedByString: @""];

//then replace the "|"

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

Comments

0

i did this to replace a string in a .plist so it might work for you

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

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

Comments

0

you may cast or convert paths to NSString and then do:

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

if this does't work, create new NSString instance that containing pathArr text, invoke replaceOccurrences method and do invert conversion

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

then use this method for tempStr. And then try:

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

may be last method not completely correct, so try experiment with it.

1 Comment

Got a warning tat saysNSMutableArray may not respond to stringByReplacingOccurrencesOfString:with String.
0

Uh, why don't you just go:

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

If there are more than one nested array, you can go

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

    // do something with cleanedString
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.