2010-07-09 2 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

가장 좋은 해결책은 확실하지 않지만 제로 다음에 공백이 있는지 확인하고 0을 따르는 경우 M, T, W, F 또는 S를 입력합니다. 그런 다음 한 시간 만에 끝나고 세미콜론으로 바꿉니다. 나는 객관적인 C를 처음 접했고, 앞으로 어떻게 들여다 보거나 NSString에서 개별 문자를 검사 할 지 모릅니다. 이것은 복잡한 해결책 일 수도 있습니다.

관련 또한이 시간은 24 시간에서 12 시간으로 변환해야합니다. 예 : 1700 ~ 17 : 00, 09 : 30 ~ 9 : 30 1200을 빼고 오후를 추가 할 수 있다는 것을 이해합니다. 그러나 시간과 분 사이에 :를 추가하고 오전 10시 이전에 선행 0을 제거하는 방법은 무엇입니까?

많은 양의 텍스트를 보내 주셔서 죄송합니다. 처음에는 더 설명하는 것이 좋을 것이라고 느꼈습니다.

건배

답변

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 

쿼리를 후속 주저하지만 당신이 관련 주제에 대한 또 다른 특정 질문이있는 경우, 앞서 가서 여기에 StackOverflow에 대한 새로운 질문하지 마십시오. 이 사이트의 주요 목표 인 검색 가능성을 높여줍니다.

+0

고마워요! 몇 시간 동안 작업 한 후에 해결책을 생각해 냈습니다.하지만 조금 더러워졌고 평범하지 않은 것을 발견하면 그다지 강력하지 않습니다. 당신의 솔루션은 더 단순 해 보입니다. 그래서 나는 그것으로 작업을 시도 할 것입니다. 내가 준수하려고하는 다른 파일의 형식은 다음과 같습니다. 월 ~ 금요일 오전 9시 30 분 ~ 오후 4시, 금요일 오전 9시 30 분 ~ 오후 5시 그래서 몇 가지 추가 정보가 있습니다. 시대와 - 사이의 공간. 저는이 부분을 어디에 넣을 지 잘 모르겠습니다. 그렇기 때문에 사람들이 따로 구해서 도움을 줄 수있는 대답으로 해결책을 게시 할 것입니다. 감사합니다. –

+0

또한 다른 곳에서 이미 시간을 잡아 당겨서 라인을 결합하지 않고 라인별로 처리해야합니다. –

0

이것은 내가 생각해 냈지만 여전히 사기 다. 문자를 삽입하여 공백 문자 (Mon-Thu 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:@";"]; 
    } 
}