2010-07-09 75 views
0

我想操纵一个给我的开放时间字符串。目标C字符串操作,偷看,添加到

它的格式不好,我需要将它与另一个来自不同来源的另一个数据相提并论。

周一至周三930-1700周四周五900-1700 930-1700
周一 - 周三930-1700周四周五900-1700 930-1700
周一 - 周四周五930-1600 930-1700
周一 - 周三930-1700周四周五900-1700周六930-1700 900-1200

正如你可以看到有没有总是在天等

我需要它由分号如下分开连字符之间的空间:

周一至周三930-1700;周四900-1700 ;周五930-1700
周一 - 周三930-1700;周四900-1700;周五930-1700
周一 - 周四930-1600;周五930-1700
周一 - 周三930-1700;周四900-1700;星期五930-1700;星期六900-1200

不确定它是否是最好的/最简单的解决方案,但我有想法检查在零之后是否有空格,并且如果在零之后是字母例如M,T, W,F或S.然后我会知道这是一组小时的结束并用分号替换空格。我是新来的目标C,真的不知道如何偷看或检查NSString中的单个字符。这似乎也可能是一个复杂的解决方案。

此外,我需要将这些小时从24小时转换为12小时。例如下午17时至5时,上午9时30分至9时30分。我知道我可以减去1200并添加pm,但是如何添加:小时和分钟之间的时间,并且如果在上午10:00之前删除前导零?

对不起大量的文字,但我觉得最好在开始的时候多说明一下。

干杯

回答

1
// 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 

不要犹豫,问随动查询,但如果你有一个相关主题的另一个具体问题,继续前进,使这里在计算器上的新问题。它使它更加可搜索,这是本网站的主要目标。

+0

谢谢! 我在工作了几个小时后想出了一个解决方案,但它有点肮脏,我认为一旦遇到不寻常的事情,它不会很健壮。你的解决方案看起来更简单,所以我认为我可以尝试这方面的工作。我可能不得不解决你的问题,因为我试图遵守的其他文件的格式是: 周一 - 周四上午9:30 - 下午4:00;周五上午9:30 - 下午5:00 因此,它还有几个时间与 - 之间的空间。 我只是新来的,所以我不知道该把这个放在哪里,但我会发布我的解决方案作为人们挑选和提供帮助的答案。 谢谢willc! – 2010-07-10 07:06:06

+0

另外,我仍然必须逐行进行,而不是将这些线条合并,因为我已经将时间从对象的其他地方拉出来 – 2010-07-10 07:17:56

0

这是我想出的东西,但它仍然是狡猾的。他们是不是一个简单的方法来寻找通过字符串找到零空间字母(如周一至周四800-1700 Fri),然后插入一个字符?

我使用了RegexKitLite,并提出了以下内容。

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