如何在 Objective C 中根据大小写(即大写字母)拆分 NSString?

发布于 2024-12-20 08:08:22 字数 287 浏览 2 评论 0原文

使用 Objective C,根据大小写拆分 NSString 的最佳方法是什么,例如 @"MyNameOfSomething" --> [NSArray arrayWithObjects: @"My", @"Name", @"Of, @"Something"].

我想在不使用第三方正则表达式库的情况下执行此操作,因为我不需要正则表达式功能对于程序中的其他内容,

我的目标是比 iOS5/Lion 更旧的版本,所以我知道这可以用 NSScanner 完成,但我希望有人有一个现成的。 解决方案。

Using Objective C, what would be the best way to split an NSString based on capitalization, e.g. @"MyNameOfSomething" --> [NSArray arrayWithObjects: @"My", @"Name", @"Of, @"Something"].

I would like to do this without using a third-party regex library, since I will not need regex functionality for anything else in the program.

UPDATE: I'm targeting older versions than iOS5/Lion, so no NSRegularExpression. I know this can be done with NSScanner, but I was hoping somebody had a ready-made solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等往事风中吹 2024-12-27 08:08:22

我知道这可以使用 NSScanner 来完成,但我希望有人有一个现成的解决方案。

干得好:

    NSString *chopString = stringToSplit.copy; /// @"stringToSplit" Goes Here
    NSString *resultingString;

    NSCharacterSet *capsSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];

    while (chopString.length > 0) {

        /// Pull the first character out each time ///
        /// Since it may trigger the Scanner ///
        NSString *firstChar = [chopString stringByReplacingCharactersInRange:(NSRange){1, chopString.length-1} withString:@""];
        chopString = [chopString stringByReplacingCharactersInRange:(NSRange){0,1} withString:@""];

        /// Scan up to the next Capital letter ///
        NSString *upToString;
        NSScanner *chopScanner = [NSScanner scannerWithString: chopString];
        [chopScanner scanUpToCharactersFromSet:capsSet intoString:&upToString];

        /// Feed first character and scan results into string ///
        /// Separated by a space ///
        if (!resultingString)
            resultingString = [NSString stringWithFormat:@"%@%@",firstChar,upToString];
        else
            resultingString = [NSString stringWithFormat:@"%@ %@%@", resultingString,firstChar,upToString];

        /// Chop the remaining string down by the length of the scan ///
        chopString = [chopString stringByReplacingCharactersInRange:(NSRange){0,chopScanner.scanLocation} withString:@""];
    }

    resultingString = [resultingString capitalizedString]; /// @"String To Split" Comes Out!

I know this can be done with NSScanner, but I was hoping somebody had a ready-made solution.

Here you go:

    NSString *chopString = stringToSplit.copy; /// @"stringToSplit" Goes Here
    NSString *resultingString;

    NSCharacterSet *capsSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];

    while (chopString.length > 0) {

        /// Pull the first character out each time ///
        /// Since it may trigger the Scanner ///
        NSString *firstChar = [chopString stringByReplacingCharactersInRange:(NSRange){1, chopString.length-1} withString:@""];
        chopString = [chopString stringByReplacingCharactersInRange:(NSRange){0,1} withString:@""];

        /// Scan up to the next Capital letter ///
        NSString *upToString;
        NSScanner *chopScanner = [NSScanner scannerWithString: chopString];
        [chopScanner scanUpToCharactersFromSet:capsSet intoString:&upToString];

        /// Feed first character and scan results into string ///
        /// Separated by a space ///
        if (!resultingString)
            resultingString = [NSString stringWithFormat:@"%@%@",firstChar,upToString];
        else
            resultingString = [NSString stringWithFormat:@"%@ %@%@", resultingString,firstChar,upToString];

        /// Chop the remaining string down by the length of the scan ///
        chopString = [chopString stringByReplacingCharactersInRange:(NSRange){0,chopScanner.scanLocation} withString:@""];
    }

    resultingString = [resultingString capitalizedString]; /// @"String To Split" Comes Out!
冷月断魂刀 2024-12-27 08:08:22

如果您使用的是 iOS5/Mac OSX 10.7,您可以使用 NSRegularExpression 来获取大写字母的位置。 (然后使用 substringWithRange:) 如果您需要向后兼容性,NSScanner 是您(效率低下)的朋友。

If you are on iOS5/Mac OSX 10.7 you can use NSRegularExpression to get the positions of capitalized letters. (and then use substringWithRange:) If you need backwards compatibility, NSScanner is your (inefficient) friend.

辞旧 2024-12-27 08:08:22

您可以使用正则表达式来执行此操作。使用 http://rubular.com 测试您的正则表达式和 RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/) 将 Regex 功能添加到您的应用程序中。

这是一个可以解决问题的正则表达式(它选择 NSString 中的所有大写字母): [AZ]

我不确定,但我认为您可以利用此方法 - ( NSArray *)componentsSeparatedByRegex:(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error; 执行您想要的操作。

这样做的好处是它可以在 10.5 上运行,而且实现起来非常简单!

You can use a regular expression to do this. Use http://rubular.com to test your Regular Expressions, and RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/) to add Regex functionnality to your application.

Here's a regular expression that should do the trick (it selects all capitals in the NSString): [A-Z]

I'm not sure, but I think you can take advantage of this method - (NSArray *)componentsSeparatedByRegex:(NSString *)regex options:(RKLRegexOptions)options range:(NSRange)range error:(NSError **)error; to do what you want.

The advantage of this is it works on 10.5, and it's really simple to implement !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文