从另外两个 NSMutableString 创建 NSMutableString

发布于 2024-11-02 14:54:17 字数 147 浏览 1 评论 0原文

我有两个字符串:

@"--U"@"-O-" 并且想创建另一个 NSMutableString 来生成 @"-OU"< /code> 使用两个给定。有谁知道我该怎么做?

I have two strings:

@"--U" and @"-O-" and would like to create another NSMutableString that makes @"-OU" using the two givens. Does anyone know how I can do this?

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

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

发布评论

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

评论(3

巷雨优美回忆 2024-11-09 14:54:17

请注意,以下代码假设 s1 和 s2 具有相同的长度,否则在某些时候会抛出异常,因此请进行检查:)

- (NSMutableString *)concatString:(NSString *)s1 withString:(NSString *)s2
{
    NSMutableString *result = [NSMutableString stringWithCapacity:[s1 length]];
    for (int i = 0; i < [s1 length]; i++) {
        unichar c = [s1 characterAtIndex:i];
        if ( c != '-' ) {
            [result appendFormat:@"%c", c];
        }
        else {
            [result appendFormat:@"%c", [s2 characterAtIndex:i]];
        }
    }
    return result;
}

Note, the following code assumes that s1 and s2 have the same length, otherwise it will throw an exception at some point, so do the checking :)

- (NSMutableString *)concatString:(NSString *)s1 withString:(NSString *)s2
{
    NSMutableString *result = [NSMutableString stringWithCapacity:[s1 length]];
    for (int i = 0; i < [s1 length]; i++) {
        unichar c = [s1 characterAtIndex:i];
        if ( c != '-' ) {
            [result appendFormat:@"%c", c];
        }
        else {
            [result appendFormat:@"%c", [s2 characterAtIndex:i]];
        }
    }
    return result;
}
£冰雨忧蓝° 2024-11-09 14:54:17
    NSString *t1=@"-0-";
    NSString *t2=@"--U";

    NSString *temp1=[t1 substringWithRange:NSMakeRange(0, 2)];
    NSString *temp2=[t2 substringFromIndex:2];
    NSLog(@"%@",[NSString stringWithFormat:@"%@%@",temp1,temp2]);
    NSString *t1=@"-0-";
    NSString *t2=@"--U";

    NSString *temp1=[t1 substringWithRange:NSMakeRange(0, 2)];
    NSString *temp2=[t2 substringFromIndex:2];
    NSLog(@"%@",[NSString stringWithFormat:@"%@%@",temp1,temp2]);
落花随流水 2024-11-09 14:54:17

这个版本比 Nick 的更冗长一些,但是将其分解为 C 函数和尾递归,因此它可能运行得更快。它还处理不同长度的字符串,选择镜像较短字符串的长度。

注意:我还没有运行此代码,因此它可能有错误或缺少一些明显的东西。

void recursiveStringMerge(unichar* string1, unichar* string2, unichar* result) {
    if (string1[0] == '\0' || string2[0] == '\0') {
        result[0] = '\0'; //properly end the string
        return; //no use in trying to add more to this string
    }
    else if (string1[0] != '-') {
        result[0] = string1[0];
    }
    else {
        result[0] = string2[0];
    }
    //move on to the next unichar in each array
    recursiveStringMerge(string1+1, string2+1, result+1);
}

- (NSMutableString *)concatString:(NSString *)s1 withString:(NSString *)s2 {
    NSUInteger resultLength;
    NSUInteger s1Length = [s1 length]+1; //ensure space for NULL with the +1
    NSUInteger s2Length = [s2 length]+1;

    resultLength = (s1Length <= s2Length) ? s1Length : s2Length; //only need the shortest

    unichar* result = malloc(resultLength*sizeof(unichar));
    unichar *string1 = calloc(s1Length, sizeof(unichar));
    [s1 getCharacters:buffer];
    unichar *string2 = calloc(s2Length, sizeof(unichar));
    [s2 getCharacters:buffer];

    recursiveStringMerge(string1, string2, result);
    return [NSString stringWithCharacters: result length: resultLength];
}

This version is a bit more long-winded than Nick's, but breaks the thing down into C functions and tail recursion, so it may run faster. It also handles strings of different lengths, choosing to mirror the shorter string's length.

NOTE: I have not run this code yet, so it may be buggy or be missing something obvious.

void recursiveStringMerge(unichar* string1, unichar* string2, unichar* result) {
    if (string1[0] == '\0' || string2[0] == '\0') {
        result[0] = '\0'; //properly end the string
        return; //no use in trying to add more to this string
    }
    else if (string1[0] != '-') {
        result[0] = string1[0];
    }
    else {
        result[0] = string2[0];
    }
    //move on to the next unichar in each array
    recursiveStringMerge(string1+1, string2+1, result+1);
}

- (NSMutableString *)concatString:(NSString *)s1 withString:(NSString *)s2 {
    NSUInteger resultLength;
    NSUInteger s1Length = [s1 length]+1; //ensure space for NULL with the +1
    NSUInteger s2Length = [s2 length]+1;

    resultLength = (s1Length <= s2Length) ? s1Length : s2Length; //only need the shortest

    unichar* result = malloc(resultLength*sizeof(unichar));
    unichar *string1 = calloc(s1Length, sizeof(unichar));
    [s1 getCharacters:buffer];
    unichar *string2 = calloc(s2Length, sizeof(unichar));
    [s2 getCharacters:buffer];

    recursiveStringMerge(string1, string2, result);
    return [NSString stringWithCharacters: result length: resultLength];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文