【问题标题】:Objective C string manipulation, peeking, adding toObjective C 字符串操作,查看,添加到
【发布时间】:2011-03-13 16:00:27
【问题描述】:

我正在尝试操纵给我的一系列开放时间。

它的格式很差,我需要将它提升到与来自不同来源的另一条数据相同的标准。

周一至周三 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 小时时间。例如,1700 到下午 5:00,0930 到上午 9:30。我知道我可以减去 1200 并添加 pm,但是如何在小时和分钟之间添加 : 并且如果它在上午 10:00 之前还删除前导零?

很抱歉文字量很大,但我觉得最好在开始时解释一下。

干杯

【问题讨论】:

    标签: objective-c cocoa string nsstring


    【解决方案1】:

    这是我想出来的,但它仍然很狡猾。他们不是更容易通过字符串查找零空格字母(如周一至周四 800-1700 周五)然后插入一个字符吗?

    我使用了 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:@";"];
        }
    }
    

    【讨论】:

      【解决方案2】:
      // 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 上提出新问题。它使其更易于搜索,这是本网站的主要目标。

      【讨论】:

      • 谢谢!在工作了几个小时后,我想出了一个解决方案,但它有点脏,我认为一旦遇到不寻常的东西,它就不是很健壮。您的解决方案看起来更简单,所以我想我可以尝试使用它。我可能不得不解决你的问题,因为我试图符合的其他文件的格式是:周一至周四上午 9:30 - 下午 4:00;周五上午 9:30 - 下午 5:00 所以它之间还有一些空格时代和-。我只是这里的新手,所以我不确定该放在哪里,但我会发布我的解决方案作为人们挑选并提供帮助的答案。谢谢你!
      • 另外,我仍然需要逐行执行,而不是合并行,因为我已经从其他地方的对象中抽出时间
      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      相关资源
      最近更新 更多