3

I am stuck in finding the solution of splitting the string into substrings. I need some proper logic in which I need to read string starting with some character and it should contain at least 17 characters.

e.g. "43 01 08 43 13 01 18 FD 48 6B D1 43 01 22 02 01 02 02 F1 48 6B D1 43 02 03 03 51 03 52 75 48 6B D1 43 03 53 11 08 15 52 9D 48 6B D1 43 16 14 16 32 00 00"

For Above string , I need to split in into "43 01 08 43 13 01 18" "43 01 22 02 01 02 02" "43 02 03 03 51 03 52" "43 03 53 11 08 15 52" "43 16 14 16 32 00 00"

So On... I mean I need to get the position of "43" ,then from its index i Need to read 20 characters (Including space) and reject the characters until the next "43" appears (BOLD characters as mentioned above). Thanks in advance .. :)

6
  • Show what you have tried?? Commented Nov 18, 2013 at 8:33
  • I believe NSScanner is the best way to handle these kind of logic. Commented Nov 18, 2013 at 8:35
  • I was trying the sample code for pastebin.com/cHpGL1Gr but if there is any response which contains the "43" in between the string then it wont work !! Commented Nov 18, 2013 at 8:35
  • You said you need to read 20 chars from the '43', but in the example you provide, it seems you read only 10... Commented Nov 18, 2013 at 9:03
  • @neutrino blankSpace is also a character !! :) Commented Nov 18, 2013 at 9:40

3 Answers 3

3

I would just use rangeOfString: to locate the next 43, take the 20 characters from there on with substringWithRange:, and start over with the remaining substring (substringFromIndex:).

NSString *str = @"43 01 08 43 13 01 18 FD 48 6B D1 43 01 22 02 01 02 02 F1 48 6B D1 43 02 03 03 51 03 52 75 48 6B D1 43 03 53 11 08 15 52 9D 48 6B D1 43 16 14 16 32 00 00";
NSMutableArray *substrings = [NSMutableArray array];
NSRange range = { .length = 20 };
while ((range.location = [str rangeOfString:@"43"].location) != NSNotFound
       && str.length >= range.length + range.location) {
    [substrings addObject:[str substringWithRange:range]];
    str = [str substringFromIndex:range.location + range.length];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

   NSMutableArray *output = [[NSMutableArray alloc] init];
    NSString *allString = @"43 01 08 43 13 01 18 FD 48 6B D1 43 01 22 02 01 02 02 F1 48 6B D1 43 02 03 03 51 03 52 75 48 6B D1 43 03 53 11 08 15 52 9D 48 6B D1 43 16 14 16 32 00 00";

    while (allString.length>0) {
        [output addObject:[allString substringToIndex:20]];
        allString = [allString substringFromIndex:20];
        NSArray *arr = [allString componentsSeparatedByString:@"43"];

       if (arr.count>1){
          allString =@"43";
          for (int i=1;i<arr.count;i++){
              allString = [allString stringByAppendingString:[arr objectAtIndex:i]];
          }
       }else{
          allString=@"";
       }

    }

Comments

1

Assuming your sequence always starts with "43" and is seven items long.

Split the string and walk down the array creating sub arrays.

-(NSArray *)splitString:(NSString *)masterString
{
    NSArray *values = [masterString componentsSeparatedByString:@" "];

    NSMutableArray *outputarray = [@[] mutableCopy];

    NSUInteger marker = 0;

    NSString *startValue = @"43";

    NSUInteger subarraycount = 7;

    BOOL foundFlag = YES;

    while (foundFlag) {

        NSRange checkRange = NSMakeRange(marker, (values.count - marker));

        NSUInteger next = [values indexOfObject:startValue inRange:checkRange];

        foundFlag = (next != NSNotFound) && ((next + subarraycount) < values.count);

        if (foundFlag) {

            NSRange subrange = NSMakeRange(next, subarraycount);

            NSArray *subarray = [values subarrayWithRange:subrange];

            [outputarray addObject:[subarray componentsJoinedByString:@" "]];

            marker = next + subarraycount;
        }

    }

    return [outputarray copy];

}

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.