views:

63

answers:

2

Hi,

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 time. 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

A: 
// 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.

willc2
Thanks!I came up with a solution after working at it for hours but it is a bit dirty and I assume not very robust once it encounters something out of the ordinary. Your solution looks more simple so I think I may try work with that. I may have to work around with yours as the format of the other file I am trying to conform with is:Mon-Thur 9:30am - 4:00pm;Fri 9:30am - 5:00pmSo it has a few more spaces between the times and the -.I'm only new here so I'm not sure where to put this but I'll post my solution as an answer for people to pick apart and offer help too.Thanks willc!
Daniel Bowden
Also I would still have to do it line by line and not combine the lines as I am already pulling the hours out from an object elsewhere
Daniel Bowden
A: 

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:@";"];
    }
}
Daniel Bowden