开发者

Objective C string manipulation, peeking, adding to

I am trying to manipulate a string of open hours that is given to me.

It is poorly formatted and I need to bring it up to the same standard as another piece of data from a different source.

Mon-Wed 930-1700 Thu 900-1700 Fri 930-1700

Mon - Wed 930-1700 Thu 900-1700 Fri 930-1700

Mon - Thu 930-1600 Fri 930-1700

Mon - Wed 930-1700 Thu 900-1700 Fri 930-1700 Sat 900-1200

As you can see there is not always spaces between hyphens on days etc.

I need it to be separated by semicolon as follows:

Mon-Wed 930-1700;Thu 900-1700;Fri 930-1700

Mon - Wed 930-1700;Thu 900-1700;Fri 930-1700

Mon - Thu 930-1600;Fri 930-1700

Mon - Wed 930-1700;Thu 900-1700;Fri 930-1700;Sat 900-1200

Not sure if its the best/easiest solution but I had the idea to check if there is a space following a zero and if following that zero is a letter eg M, T, W, F, or S. Then I would know it is the end of one set of hours and replace the space with a semicolon. I am new to objective c and really don't know how to peek ahead or check individual characters in a NSString. This also seems like it may be a complicated solution.

Also related, I need to convert these hours from 24hr time to 12hr ti开发者_运维问答me. eg 1700 to 5:00pm, 0930 to 9:30am. I understand I can subtract 1200 and add the pm but how do I add the : between hour and minute and also remove the leading zero if it is before 10:00am?

Sorry for the large amount of text but I felt it was better to explain it more in the beginning.

Cheers


// side note: you should probably not mess around with individual characters unless you're only dealing with ASCII

NSString *text = // all your text  

NSMutableString *mutableText = [[text mutableCopy] autorelease];  // make a mutable copy so we can change in place

[mutableText replaceOccurrencesOfString:@"\t" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableText length])]; // delete Tabs
[mutableText replaceOccurrencesOfString:@" -" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableText length])]; // make all dashes consistent
[mutableText replaceOccurrencesOfString:@"- " withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableText length])];

NSArray *words = [mutableText componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; // split each line

NSMutableString *cleanedText = [NSMutableString stringWithCapacity:0]; // will hold cleaned-up string

// go thru each line and insert semi-colon after all but the last hours 
for (NSString *record in words)
{ 
    // assumes all hours end in zero
    NSString *newRecord = [record stringByReplacingOccurrencesOfString:@"0 " withString:@"0;" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [record length])];
    [cleanedText appendFormat:@"%@\n", newRecord];
}

NSLog(@"%@", cleanedText); // all done

Don't hesitate to ask followup queries but if you have another specific question on a related topic, go ahead and make it a new question here on StackOverflow. It makes it more searchable, which is a main goal of this site.


This is something I came up with but it is still dodgy. Is their not an easier way to seek through the string to find zero space letter (as in Mon-Thu 800-1700 Fri) and then insert a character?

I used the RegexKitLite and have come up with the following.

NSString *regexString = @"(\\d\\d\\d\\s[A-Za-z])"; //look for 3 numbers a space and a letter

NSRange matchedRange = NSMakeRange(NSNotFound,NSNotFound);

//clean other common problems in data
NSString *outputString = [hoursString stringByReplacingOccurrencesOfString:@"," withString:@""]; 

outputString = [outputString stringByReplacingOccurrencesOfString:@"." withString:@""];

outputString = [outputString stringByReplacingOccurrencesOfString:@" &" withString:@""];

NSRange replaceRange;

while (!matchedRange.length == 0) { //loop while not all matches in the one string found and fixed

    matchedRange = [outputString rangeOfRegex:regexString capture: 1];

    if (!matchedRange.length == 0) {

       //we want to add a semicolon 3 characters after the first found number
        replaceRange = NSMakeRange(matchedRange.location+3, 1);
       //replace space with ;
        outputString = [outputString stringByReplacingCharactersInRange:replaceRange withString:@";"];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜