2

I have a string..

NSString* string = @"%B999999^PDVS123456789012^PADILLA L.                    ^0X0000399           ?*;999999554749123456789012=00X990300000?*

What I want is to get the name PADILLA L. and 999999554749123456789012=00X990300000?*

3 Answers 3

4

Use NSString componentsSeparatedByString: to split the string up. First use @"^". The name will be at index 2. Then split the substring at index 3 using @";". The string at index 1 will give you the 2nd piece you want.

NSArray *substrings = [string componentsSeparatedByString:@"^"];
NSString *name = substrings[2];
name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *lastpart = substrings[3];
NSArray *moresubstrings = [lastpart componentsSeparatedByString:@";"];
NSString *secondPiece = moresubstrings[1];
Sign up to request clarification or add additional context in comments.

Comments

0

Without more specifics here is a brute force way:

NSString* string = @"%B999999^PDVS123456789012^PADILLA L.                    ^0X0000399           ?*;999999554749123456789012=00X990300000?*";
NSRange nameRange = {26, 10};
NSString *name = [string substringWithRange:nameRange];
NSRange numRange = {80, 39};
NSString *num = [string substringWithRange:numRange];

The documentation is your friend: NSString Class Reference

2 Comments

Keep in mind that this solution is only valid if the data is guaranteed to be fixed length.
yes. exactly. I mean I appreciate your answer but hardcoding the range would not be very wise. But nevertheless thank you for your answer. :)
0

Without knowing what the exact input pattern is (we have your n-of-1 example only), it's going to hard to say exactly how you might parse this properly; but NSRegularExpression offers what you need (in addition to other suggested approaches):

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSString *sampleText = @"%B999999^PDVS123456789012^PADILLA L.                    ^0X0000399           ?*;999999554749123456789012=00X990300000?*";
        NSError *regexError = nil;
        NSRegularExpressionOptions options = 0;
        NSString *pattern = @"^%\\w+\\^\\w+\\^([A-Za-z\\s]+\\.).+\\?\\*\\;(.+)\\?\\*$";
        NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:options error:&regexError];

        NSTextCheckingResult *match = [expression firstMatchInString:sampleText options:0 range:range];
        if( match ) {
            NSRange nameRange = [match rangeAtIndex:1];
            NSRange numberRange = [match rangeAtIndex:2];

            printf("name = %s ",[[sampleText substringWithRange:nameRange] UTF8String]);
            printf("number = %s\n",[[sampleText substringWithRange:numberRange] UTF8String]);
        }
    }
}

This little Foundation application prints the following to the console:

name = PADILLA L. number = 999999554749123456789012=00X990300000

The regex used to analyze the input string may need to be tweaked depending on how the input string varies. Right now it is (unescaped):

^%\w+\^\w+\^([A-Za-z\s]+\.).+\?\*\;(.+)\?\*$

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.