【问题标题】:How do I convert a string to camelcase如何将字符串转换为驼峰式
【发布时间】:2019-05-25 03:46:48
【问题描述】:

我得到一个字符串,我必须将其转换为 Camel Case,并使用 main 中的调用从我的函数返回结果值。

// CaseMaker.h


    - (instancetype)initWithString:(NSString *)string;

    - (NSString *)process;

    @end

// main.m

    #import "CaseMaker.h"

    int main(int argc, const char * argv[]) {
  @autoreleasepool {
    CaseMaker *maker1 = [[CaseMaker alloc] initWithString:@"this is a string"];
    NSLog(@"%@", [maker1 process]);

    CaseMaker *maker2 = [[CaseMaker alloc] initWithString:@"loopy lighthouse"];
    NSLog(@"%@", [maker2 process]);
  }
  return 0;
}

我已经能够将带有空格的字符串转换为大写字符,但我不能将第一个字符设为小写,而且我正在大写我不想做的单个单词字符串。 NSString 的文档并没有像我希望的那样有帮助

.m

    - (NSString *)camelCaseFromString:(NSString *)input
    {
    return [[input capitalizedString]stringByReplacingOccurrencesOfString:@" " withString:@""];
    }

主要

    CaseMaker *maker1 = [[CaseMaker alloc] camelCaseFromString:@"this is a string"];
    NSLog(@"%@", maker1);
    CaseMaker *maker2 = [[CaseMaker alloc] camelCaseFromString:@"loopy lighthouse"];
    NSLog(@"%@", maker2);
    CaseMaker *maker3 = [[CaseMaker alloc] camelCaseFromString:@"supercalifragalisticexpialidocious"];
    NSLog(@"%@", maker3);       
    CaseMaker *maker4 = [[CaseMaker alloc]camelCaseFromString:@"HELLO BRO"];
    NSLog(@"%@",maker4);

thisIsAString
loopy灯塔
supercalifragalisticexpialidocious

以上是此作业的预期输出。我在google-fu-ing,看看其他人是如何解决这个问题的,但真的找不到任何东西,阅读了一些关于NSString的大写/小写方法的客观c文档,但仍然对我应该如何继续感到困惑

【问题讨论】:

    标签: objective-c nsstring camelcasing


    【解决方案1】:

    这是一个小的 NSString 类别,它添加了一个 toCamelCase 方法。

    NSString+Util.h:

    #import <Foundation/Foundation.h>
    
    @interface NSString(Util)
    
    @property (readonly, copy) NSString *camelcaseString;
    
    @end
    

    NSString+Util.m:

    #import "NSString+Util.h"
    
    @implementation NSString(Util)
    
    - (NSString *)camelcaseString {
        NSMutableString *res = [NSMutableString string];
        [[self componentsSeparatedByString:@" "] enumerateObjectsUsingBlock:^(NSString * _Nonnull string, NSUInteger idx, BOOL * _Nonnull stop) {
            [res appendString:idx == 0 ? [string lowercaseString] : [string capitalizedString]];
        }];
    
        return [res copy];
    }
    
    @end
    

    用法:

    NSLog(@"%@", @"this is a string".camelcaseString);
    

    输出:

    thisIsAString


    在 Swift 中,您可以为 StringProtocol 创建一个扩展:

    extension StringProtocol {
        var camelcased: String {
            return components(separatedBy: " ").enumerated().map { $0 == 0 ? $1.lowercased() : $1.capitalized }.joined(separator: "")
        }
    }
    

    用法:

    print("this is a string".camelcased)
    

    【讨论】:

    • [typo] 我认为您可能在 Swift 翻译中错过了管道中的一个操作。
    • @CRD Swift 代码给出了正确的结果。我错过了什么?
    • ObjC 中有一个小写操作
    • @CRD 不错。谢谢。已修复(两个版本)。
    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 2011-10-03
    • 1970-01-01
    • 2023-02-20
    • 2018-05-29
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多