将数组复制到字符串并按顺序显示字符串

发布于 2024-12-24 20:14:38 字数 98 浏览 3 评论 0原文

我正在将 NSMutableArray 复制到字符串。当我显示字符串时,我在数组项之前得到一个“(”符号,数组项之间用逗号分隔。我想逐行显示数组项,而不是用逗号分隔。我怎样才能这样做

I am copying an NSMutableArray to a string. When I am displaying the string I am getting a "(" sign before the array items and the array entries are separated by a comma in between. I want to display the array entries line by line, and not by comma separated. How can I do this

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

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

发布评论

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

评论(3

_畞蕅 2024-12-31 20:14:38

有多种方法可以做到这一点。如果您只想用换行符加入数组,最简单的方法是使用 NSArray-componentsJoinedByString: 方法。例如,要完全按照您的要求进行操作:

NSArray* myArray = // assume this exists
NSString* stringJoinedByNewLines = [myArray componentsJoinedByString:@"\n"];
// This should show each of the elements separated by a new-line (and they are now in a single string)
NSLog(@"the string: %@", stringJoinedByNewLines);

There are a number of ways to do this. If you just want to join the array with a new-line character, the easiest is to use NSArray's -componentsJoinedByString: method. For example, to do exactly what you asked:

NSArray* myArray = // assume this exists
NSString* stringJoinedByNewLines = [myArray componentsJoinedByString:@"\n"];
// This should show each of the elements separated by a new-line (and they are now in a single string)
NSLog(@"the string: %@", stringJoinedByNewLines);
晨曦慕雪 2024-12-31 20:14:38
 NSMutableArray * items = someArray;
    NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*10];
    for (NSString * s in items)
    {
        [bulletList appendFormat:@"%@\n", s];
    }

yourTextView.text = bulletList;
 NSMutableArray * items = someArray;
    NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*10];
    for (NSString * s in items)
    {
        [bulletList appendFormat:@"%@\n", s];
    }

yourTextView.text = bulletList;
最好是你 2024-12-31 20:14:38

您可以尝试

NSString*str=[str1 stringByReplacingOccurrencesOfString:@"(" withString:@"\n"];

这将用新行字符替换所有左大括号。对右大括号执行相同的操作。

You can try

NSString*str=[str1 stringByReplacingOccurrencesOfString:@"(" withString:@"\n"];

This will replace all the opening braces with a new line character.Do the same for closing brace.

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