Ios stringWithFormat返回换行符
通过从表视图中选择选项并在文本字段中查看,我将对象添加到可变数组中。当我使用stringWithFormat时,代码行会自动添加字符 示例:我从表视图中选择BobIos stringWithFormat返回换行符,ios,objective-c,Ios,Objective C,通过从表视图中选择选项并在文本字段中查看,我将对象添加到可变数组中。当我使用stringWithFormat时,代码行会自动添加字符 示例:我从表视图中选择Bob Bob 当我做一个NSLog时,它实际上显示为 ( Bob ) 但文本字段中出现的是 ( Bob) 因为有 (\n Bob\n) 下面是我用来去掉括号并用分号替换逗号的代码 // All Names is the mutable array I am adding informati
Bob
当我做一个NSLog时,它实际上显示为
(
Bob
)
但文本字段中出现的是
( Bob)
因为有
(\n Bob\n)
下面是我用来去掉括号并用分号替换逗号的代码
// All Names is the mutable array I am adding information in. tableData is another mutable array with set names in them
[AllNames addObject:tableData[0]];
// If I chose the first option
// NSString
ourForces = [NSString stringWithFormat: AllNames[0]];
// NSString
combinedForces = [ourForces stringByReplacingOccurrencesOfString:@"," withString:@";"];
// NSString
twoCombindForces = [combinedForces stringByReplacingOccurrencesOfString:@"(" withString:@""];
// NSString
UltmateCombinedForces = [twoCombindForces stringByReplacingOccurrencesOfString:@")" withString:@""];
//personnel is the text field
personnel.text = UltmateCombinedForces;
现在的问题是:哪条路不那么混乱
( Bob)
假扮
Bob
在我的文本字段中
解决方案更新:在以下行之后:
// All Names is the mutable array I am adding information in. tableData is another mutable array with set names in them
[AllNames addObject:tableData[0]];
// If I chose the first option
// NSString
ourForces = [NSString stringWithFormat: AllNames[0]];
包括以下代码行:
personnel.text = [AllNames componentsJoinedByString:@";"];
这将删除字段中显示的(\n Bob\n)额外字符。谢谢你们的帮助和智慧 填充全名数组后使用:
personnel.text = [AllNames componentsJoinedByString:@";"];
您可以使用NSCharacter Set删除不需要的项目。。下面可能会提供一个不那么麻烦的解决方案
NSString *originalText = @"( Bob) ";
NSMutableCharacterSet *unwantedChars = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
[unwantedChars addCharactersInString:@"()"];
NSString *refinedText = [[originalText componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString:@""];
在NSLog中输入输出表达式
(
Bob
)
表示一个数组。去掉括号和换行符,而不是
NSLog(@"%@", AllNames[0]);
写
…请使用以小写字母开头的变量名。好的,我想我明白你现在想做什么了 您有一个名称数组
NSArray *names = @[@"Bob", @"Sam", @"Dave"];
你想要一个包含所有这些名字的字符串
@"Bob; Sam; Dave"
你好像在用分号把它们分开;对吗
你可以用
NSString *nameList = [names componentsJoinedByString:@"; "];
但我不能完全肯定这就是你想要达到的目标。鲍勃是什么?你用什么代码注销Bob。您使用什么代码来创建它?等等,@Fogmeister,“Bob”只是我在表视图中显示的一个人的名字。tableData=[NSArray arrayWithObjects:@“Bob”,nil];文本字段?它只能容纳一行文本。使用UILabel或UITextView。好的,那么您使用什么代码将其登录到控制台?@Fogmeister,您是在问我代码行使用的是什么void方法吗?好的,我有一个表视图,上面有一个人员列表,无论是谁,都必须填写表单。可能只有一个人;可能有六个人。当他们在表视图中选择X数量的人时,我希望所选的人出现在上面的文本字段中。我现在正在实施它。我会在五分钟内更新我的帖子。我想说谢谢你的耐心和对我问题的帮助。我非常感激。谢谢关于小写变量名,您完全正确。小写是编程的有效方法。谢谢你的建议我在Google上看到了whitespaceAndNewLineCharacterSet代码行,并决定采用另一种方式,但感谢您提供了这行新代码。我会在以后的项目中记住这一点,谢谢。
NSString *nameList = [names componentsJoinedByString:@"; "];