3

Wanted to find the best programming approach in iOS to manipulate and process text strings. Thanks!

Would like to take a file with strings to manipulate the characters similar to the following:

NQXB26JT1RKLP9VHarren              Daggett B0BMAF00SSQ   ME03B98TBAA8D

NBQB25KT1RKLP05Billison             Whiner X0AMAF00UWE   8E21B98TBAF8W

...

...

...

Each string would process in series then loop to the next string, etc.

Strip out the name and the following strings:

Take the following 3 string fragments and convert to another number base. Have the code to process the new result but unsure of how to send these short strings to be processed in series.

QXB26

B0BM

BAA8

Then output the results to a file. The xxx represents the converted numbers.

xxxxxxxxx   Harren Daggett   xxxxxxxx  xxxxxxxx

xxxxxxxxx   Billison  Whiner xxxxxxxx  xxxxxxxx

...

...

...

The end result would be pulling parts of strings out of the first file and create a new file with the desired result.

Ankit
  • 6,772
  • 11
  • 48
  • 84
  • 1
    Check with [substringToIndex](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/substringToIndex:), [substringFromIndex](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/substringFromIndex:) and [other NSString methods](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) – iDev Feb 14 '13 at 01:31

1 Answers1

2

There are several ways to accomplish what you are after, but if you want something simple and reasonably easy to debug, you could simply split up each record by the fixed position of each of the fields you have identified (the numbers, the name), then use a simple regular expression replace to condense the name and put it all back together.

For purposes like this I prefer a simple (and even a bit pedestrian) solution that is easy to follow and debug, so this example is not optimised:

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *URLs = [fm URLsForDirectory: NSDocumentDirectory
                           inDomains: NSUserDomainMask];

NSURL *workingdirURL = URLs.lastObject;
NSURL *inputFileURL  = [workingdirURL URLByAppendingPathComponent:@"input.txt" isDirectory:NO];
NSURL *outputFileURL = [workingdirURL URLByAppendingPathComponent:@"output.txt" isDirectory:NO];

// For the purpose of this example, just read it all in one chunk
NSError *error;
NSString *stringFromFileAtURL = [[NSString alloc]
                                 initWithContentsOfURL:inputFileURL
                                 encoding:NSUTF8StringEncoding
                                 error:&error];

if ( !stringFromFileAtURL) {
    // Error, do something more intelligent that just returning
    return;
}


NSArray *records = [stringFromFileAtURL componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];

NSMutableArray *newRecords = [NSMutableArray array];

for (NSString *record in records) {

    NSString *firstNumberString  = [record substringWithRange:NSMakeRange(1, 5)];
    NSString *nameString         = [record substringWithRange:NSMakeRange(15, 27)];
    NSString *secondNumberString = [record substringWithRange:NSMakeRange(43, 4)];
    NSString *thirdNumberString  = [record substringWithRange:NSMakeRange(65, 4)];

    NSString *condensedNameString = [nameString stringByReplacingOccurrencesOfString:@" +"
                                                                          withString:@" "
                                                                             options:NSRegularExpressionSearch
                                                                               range:NSMakeRange(0, nameString.length)];

    NSString *newRecord = [NSString stringWithFormat: @"%@ %@ %@ %@",
                           convertNumberString(firstNumberString),
                           condensedNameString,
                           convertNumberString(secondNumberString),
                           convertNumberString(thirdNumberString) ];

    [newRecords addObject: newRecord];
}

NSString *outputString = [newRecords componentsJoinedByString:@"\n"];

[outputString writeToURL: outputFileURL
              atomically: YES
                encoding: NSUTF8StringEncoding
                   error: &error];

In this example convertNumberString is a plain C function that converts your number strings. It could of course also be a method, depending on the architecture or your preferences.

Monolo
  • 18,205
  • 17
  • 69
  • 103