ios 如何在iphone的一个字符串中添加连接多个NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12156780/
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
how to add concatenate multiple NSString in one String in iphone
提问by user1619187
I have 5 String i want that they must be store in singe NSString all the values separate with |
sign
我有 5 个字符串,我希望它们必须存储在单个 NSString 中,所有值都用|
符号分隔
NSString *first=@"Ali";
NSString *second=@"Imran";
NSString *third=@"AliImran";
NSString *fourth=@"ImranAli";
NSString *fifth=@"Ali Imran Jamshed";
I want to all these in single NSString to store and all values separated by given sign.
我想在单个 NSString 中存储所有这些,并且所有值都由给定的符号分隔。
回答by DrummerB
NSArray *myStrings = [[NSArray alloc] initWithObjects:first, second, third, fourth, fifth, nil];
NSString *joinedString = [myStrings componentsJoinedByString:@"|"];
// release myStrings if not using ARC.
回答by Ha cong Thuan
you can try ....
NSString *joinString=[NSString stringWithFormat:@"%@|%@|%@|%@|%@",youstring1,youstring2,youstring3,youstring4,youstring5];
回答by Roman
Short solution:
简短的解决方案:
NSString *str = [@[nstring1, nstring2, nstring3] componentsJoinedByString:@","];
回答by Nitish
I guess what DrummerB suggested, is the best way. You have to store the strings in data structure. Array or dictionary for that matter.
If you just want to use strings it is not impossible, but it will be unwise. Here you go :
我猜 DrummerB 建议的是最好的方法。您必须将字符串存储在数据结构中。数组或字典。
如果你只是想使用字符串,这不是不可能的,但这是不明智的。干得好 :
NSString*first=@"Ali";
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"Imran"];
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"AliImran"];
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"ImranAli"];
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"Ali Imran Jamshed"];
回答by Tendulkar
NSArray *stringsArray = [[NSArray alloc] initWithObjects:first, second, third, fourth, fifth, nil];
NSString *combinedString = [stringsArray componentsJoinedByString:@","];
The combined String looks like this @"Ali,Imran,AliImran,ImranAli,Ali Imran Jamshed"
;
组合后的字符串如下所示@"Ali,Imran,AliImran,ImranAli,Ali Imran Jamshed"
;
回答by suryashekhar
To improve on Nitish's answer, you can reduce the number of lines by following this:
要改进 Nitish 的答案,您可以按照以下步骤减少行数:
NSString *first=@"Ali";
first = [first stringByAppendingString:[@"|" stringByAppendingString:[@"Imran" stringByAppendingString:@"|"]]];
.