问题描述
我正在尝试操纵给我的一连串开放时间。
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.
星期一至星期三930-1700星期四900-1700星期五930-1700
星期一-星期三930- 1700星期四900-1700星期五930-1700
星期一-星期四930-1600星期五930-1700
星期一-星期三930-1700星期四900-1700星期五930-1700星期六900- 1200
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:
周一至周三930-1700;周四900-1700;周五930-1700
周一至周三930- 1700;星期四900-1700;星期五930-1700
星期一-星期四930-1600;星期五930-1700
星期一-星期三930-1700;星期四900-1700;星期四930 -1700; Sat 900-1200
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
不知道它是否是最好/最简单的解决方案,但我有一个想法来检查是否在零后面是否有空格,以及是否跟随该空格零是字母,例如M,T,W,F或S然后,我会知道这是一组小时的结束,然后用分号代替空格。我是目标c的新手,真的不知道如何偷看或检查NSString中的各个字符。似乎也可能是一个复杂的解决方案。
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.
另外,我需要将这些时间从24小时转换为12小时。例如1700至5:00 pm、0930至9:30 am。我知道我可以减去1200并加上pm,但是如何在小时和分钟之间加上:,如果上午10:00之前的前导零也要删除呢?
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.
欢呼声
推荐答案
// 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
请随时询问后续查询,但如果您对相关主题,继续并在StackOverflow上提出新问题。它使它更易于搜索,这是该网站的主要目标。
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.
这篇关于目标C字符串操作,偷看,添加到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!