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.

4

1 回答 1

2

有几种方法可以完成您所追求的目标,但是如果您想要一些简单且相当容易调试的东西,您可以简单地通过您已识别的每个字段(数字、名称)的固定位置来拆分每条记录,然后使用简单的正则表达式替换来压缩名称并将它们重新组合在一起。

出于这样的目的,我更喜欢一个简单(甚至有点笨拙)的解决方案,该解决方案易于遵循和调试,因此未优化此示例:

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

在这个例子convertNumberString中是一个普通的 C 函数,它可以转换你的数字字符串。当然,它也可以是一种方法,具体取决于架构或您的偏好。

于 2013-02-15T10:26:49.167 回答