首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复杂的Objective-C字符串替换

复杂的Objective-C字符串替换
EN

Stack Overflow用户
提问于 2011-04-11 19:53:34
回答 1查看 320关注 0票数 0

我正在尝试编写一个用于字符串转换的泛型方法(目的是为RESTful应用编程接口编写一个解析器)。

消息的目的是转换字符串,如下所示

creationTSZ -> creation_tsz

userId -> user_id

该消息处理转换userId -> user_id,当前低效地循环通过字符串和更改部分。

它还不能处理creationTSZ -> creation_tsz,我认为进一步循环是非常低效的,我想知道有没有更好的方法来做这件事?

可能是Regex?

代码语言:javascript
复制
-(NSString *)fieldsQueryString 
{

    NSArray *fieldNames = [self fieldList];

    /* Final composed string sent to Etsy */
    NSMutableString *fieldString = [[[NSMutableString alloc] init] autorelease];

    /* Characters that we replace with _lowerCase */
    NSArray *replaceableChars = [NSArray arrayWithObjects:
                                 @"Q", @"W", @"E", @"R", @"T", @"Y", @"U", @"I", @"O", @"P", 
                                 @"A", @"S", @"D", @"F", @"G", @"H", @"J", @"K", @"L",
                                 @"Z", @"X", @"C", @"V", @"B", @"N", @"M", nil];

    /* Reusable pointer for string replacements */
    NSMutableString *fieldNameString = nil;

    /* Loop through the array returned by the filter and change the names */
    for(NSString *fieldName in fieldNames) {
        /* Loop if the field is to be omited */
        if ([[self valueForKey:fieldName] boolValue] == NO) continue;
        /* Otherwise change the name to a field and add it */
        fieldNameString = [fieldName mutableCopy];
        for(NSString *replaceableChar in replaceableChars) {
            [fieldNameString replaceOccurrencesOfString:replaceableChar 
                                             withString:[NSString stringWithFormat:@"_%@", [replaceableChar lowercaseString]] 
                                                options:0 
                                                  range:NSMakeRange(0, [fieldNameString length])];
        }
        [fieldString appendFormat:@"%@,", fieldNameString];
        [fieldNameString release];
    }
    fieldNames = nil;

    /* Return the string without the last comma */
    return [fieldString substringToIndex:[fieldString length] - 1];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-11 20:25:07

假设您的标识符的结构如下

代码语言:javascript
复制
<lowercase-prefix><Uppercase-char-and-remainder>

您可以使用:

代码语言:javascript
复制
NSScaner *scanner = [NSScanner scannerWithString:fieldName];
NSString *prefix = nil;
[scanner scanCharactersFromSet:[NSCharacterSet lowercaseLetterCharacterSet] intoString:&prefix];
NSString *suffix = nil;
[scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&suffix];
NSString *fieldNameString = [NSString stringWithFormat:@"%@_%@", prefix, [suffix lowercaseString]];

这将执行字段标识符的转换(但您应该执行一些错误检查,以防前缀或后缀保持为空)。

构建fieldNames列表的最简单方法是将它们添加到NSMutableArray中,然后加入它们:

代码语言:javascript
复制
NSMutableArray *fields = [[NSMutableArray alloc] init];
for (NSString *fieldName in [self fieldList]) {
    // code as above
    [fields add:fieldNameString];
}
NSString *commaFields = [fields componentsJoinedByString:@","];
[fields release];
return commaFields;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5620858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档