1

I have a text file looking like this :

#AAA:x
12
34
7
...
#BBB:y
-74.7
-33.2
14
...
#CCC:z
32.4
17
...
#END

I'm able to put all of it in one big NSArray (using componentsSeparatedByString:@"\n")

Now I'd like to have:

AAA float NSArray with all the values under the tag #AAA:x;

BBB float NSArray with all the values under the tag #BBB:y; etc..

How can I do this?

3
  • 3
    Did you consider splitting at #, then splitting at \n and ignoring the first line? Commented Oct 26, 2012 at 14:35
  • i think this could actually works ! But how can I split an NSArray in 3NSArray based on a char value ('#' here) ? Commented Oct 26, 2012 at 14:43
  • Sorry but I don't understand your answer :s In apple documentation I only saw : "subarrayWithRange(NSRange arg0)" that sounds close to what I wanna do but it uses NSrange instead of NDString Commented Oct 26, 2012 at 14:47

1 Answer 1

3

To elaborate on my comment, try this:

NSMutableArray *subarrays = [[myTest componentsSeparatedByString:@"#"] mutableCopy];
for (int i = 0; i < subarray.length; i++) {
    NSArray *subarray = [subarrays[i] componentsSeparatedByString:@"\n"];
    subarray = [subarray subarrayWithRange:NSMakeRange(1, subarray.length-1)];
    subarrays[i] = subarray;
}

This should result in an array of string arrays.

So subarrays[0] will be an array of strings with these elements: 12, 34, 7. subarrays[1][2] will be a string "14"

If you want floats and not strings, you will have to additionally iterate over all the entries and convert them to float. You can use NSString's floatValue method to do that.

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

2 Comments

Thenk you for your help. Few modifications to make on your example (if someone need them one day) NSMutableArray *subarrays = [[myTest componentsSeparatedByString:@"#"]mutablecopy]; and NSArray *subarray = [[subarrays objectAtIndex:i] componentsSeparatedByString:@"\n"];
I added your first suggestion to my post. The second one should work however as I suggested since Xcode 4.4.

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.