如何在 Objective-C (iPhone) 中连接字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1158860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:56:33  来源:igfitidea点击:

How to Concatenate String in Objective-C (iPhone)?

iphoneobjective-cstringstring-concatenation

提问by r0ach

Possible Duplicate:
How do I concatenate strings in Objective-C?

可能的重复:
如何在 Objective-C 中连接字符串?

Firstly, the platform is iPhone and label.text changes the label displayed. Consider this scenario:

首先,平台是 iPhone 并且 label.text 更改显示的标签。考虑这个场景:

I've an array of integers. And I want to display it on the screen.

我有一个整数数组。我想在屏幕上显示它。

Here's my take on it:

这是我的看法:

-(IBAction) updateText: (id)sender {
   int a[2];
   a[0]=1;
   a[1]=2;
   a[2]=3;
   for (int i=0; i<=10;i++)
     label.text = [NSString stringByAppendingString: [NSString stringWithFormat: @"%i", a[i]]]; 
}

As you can probably see, I'm pretty confused. Pls pls help me out :(

正如你可能看到的,我很困惑。请帮助我:(

回答by Tom Dalling

Try this:

尝试这个:

NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
    [theString appendString:[NSString stringWithFormat:@"%i ",i]];
}
label.text = theString;

回答by Rob Napier

Since you're using a loop, do be somewhat careful with both Tom and Benjie's solutions. They each create an extra autoreleased object per iteration. For a small loop, that's fine, but if the size of the loop is unbounded or if the strings are large, this can lead to a very large memory spike and performance hit. Particularly on iPhone, this is exactly the kind of loop that can lead to surprising memory problems due to short-lived memory spikes.

由于您使用的是循环,因此在使用 Tom 和 Benji 的解决方案时一定要小心。他们每个人每次迭代都会创建一个额外的自动释放对象。对于小循环,这很好,但是如果循环的大小是无界的或者字符串很大,这可能会导致非常大的内存峰值和性能下降。特别是在 iPhone 上,这种循环可能会由于短暂的内存峰值而导致令人惊讶的内存问题。

The following solution has a smaller memory footprint (it's also slightly faster and takes less typing). Note the call to -appendFormat:rather than -appendString. This avoids creating a second string that will be thrown away. Remember that the final string has an extra space at the end that you may want to get rid of. You can fix that by either treating the first or last iteration differently, or by trimming the last space after the loop.

以下解决方案具有较小的内存占用(它也稍微快一些并且需要较少的输入)。请注意对-appendFormat:而不是的调用-appendString。这避免了创建将被丢弃的第二个字符串。请记住,最后一个字符串的末尾有一个额外的空格,您可能想要摆脱它。您可以通过以不同方式处理第一次或最后一次迭代,或通过修剪循环后的最后一个空格来解决该问题。

NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
    [theString appendFormat:@"%i ",i];
}
label.text = theString;

Don't forget [NSArray componentsJoinedByString:]. In this case you don't have an NSArray, but in the common cases where you do, this is probably the best way to get what you're looking for.

不要忘记[NSArray componentsJoinedByString:]。在这种情况下,您没有 NSArray,但在您使用的常见情况下,这可能是获得所需内容的最佳方式。

回答by Kit

//NSArray *chunks   
string = [chunks componentsJoinedByString: @","];

回答by Benjie

Another method without using NSMutableString:

另一种不使用 NSMutableString 的方法:

NSString* theString = @"";
for (int i=0; i<=10;i++){
    theString = [theString stringByAppendingFormat:@"%i ",i];
}
label.text = theString;

Here's a full implementation (correcting your ranges):

这是一个完整的实现(更正您的范围):

-(IBAction) updateText: (id)sender {
     int a[3];
     a[0]=1;
     a[1]=2;
     a[2]=3;
     NSString *str = @"";
     for (int i=0; i<3;i++)
       str = [str stringByAppendingFormat:@"%i ",i];
     label.text = str;
}

You could also do it like this (e.g. if you wanted a comma separated list):

你也可以这样做(例如,如果你想要一个逗号分隔的列表):

-(IBAction) updateText: (id)sender {
     int a[3];
     a[0]=1;
     a[1]=2;
     a[2]=3;
     NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];
     for (int i=0; i<3;i++)
         [arr addObject:[NSString stringWithFormat:@"%i",i]];

     label.text = [arr componentsJoinedByString:@", "];
}