【问题标题】:iPhone dev: Replace uppercase characters in NSString with space and downcaseiPhone dev:用空格和小写替换 NSString 中的大写字符
【发布时间】:2010-04-08 06:53:44
【问题描述】:

我有:

NSString *promise = @"thereAreOtherWorldsThanThese";

我正在尝试将其转换为字符串:

@"There are other worlds than these"

我猜这是一个正则表达式的工作,但我对 Objective C 很陌生,到目前为止还没有运气。我将非常感谢任何帮助!

【问题讨论】:

    标签: iphone regex string replace nsstring


    【解决方案1】:

    我会使用GTMRegex (http://code.google.com/p/google-toolbox-for-mac/),例如:

    NSString *promise = @"thereAreOtherWorldsThanThese";
    GTMRegex *regex = [GTMRegex regexWithPattern:@"([A-Z])"];
    NSLog(@"%@", [[regex stringByReplacingMatchesInString:promise
                         withReplacement:@" \\1"] lowercaseString]);
    

    【讨论】:

      【解决方案2】:

      至于删除大写字母,您可以简单地在NSString 上使用lowercaseString

      但至于在大写字母之前插入空格,我同意这将是一个正则表达式的工作,可悲的是,我的正则表达式 fu 是垃圾:)

      【讨论】:

        【解决方案3】:

        无需使用任何库,您就可以使用我发布的这个 NSString 类别。只需对字符串数组执行 lowerCaseString 即可。 How do I convert an NSString from CamelCase to TitleCase, 'playerName' into 'Player Name'?

        【讨论】:

          猜你喜欢
          • 2021-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-31
          • 2014-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多