将多个字符串添加到字符串中
问题描述:
如何将多个字符串添加到字符串中? 最简单的方法是什么? 如果我不想每次我添加了一些字符串时创建新的代码行,我想要做这样的事情:将多个字符串添加到字符串中
NSString *recipeTitle = [@"<h5>Recipe name: " stringByAppendingFormat:recipe.name, @"</h5>"];
NSLog(@"%@", recipeTitle);
// This shows: <h5>Recipe name: myrecipe
// Where's the </h5> closing that header ? It will only show up with the next line of code
recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];
//my problem is that will result in more than 1k lines of programming
我一定要一定添加一个新行追加每次都附上? 有没有更快捷/更有成效的方式来做到这一点?
我想用我的tableview在其中编写电子邮件正文,这将导致一组巨大的编程线。有没有人可以给我任何提示或任何比编写huuuge字符串更好的东西,所以我可以用包含我的tableview数据的表填充我的电子邮件正文?
任何帮助,使这个更高效的赞赏。谢谢 ! 卡洛斯法里尼。
//它的工作一点我弄了:
-(IBAction)sendmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
NSString *recipeTitle = @"<h5>Recipe name: ";
recipeTitle = [recipeTitle stringByAppendingFormat:recipe.name];
recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];
NSString *ingredientAmount = @"";
NSString *ingredientAisle = @"";
NSString *ingredientTitle = @"";
NSString *tableFirstLine = @"<table width='90%' border='1'><tr><td>Ingredient</td><td>Amount</td><td>Aisle</td></tr>";
NSString *increments = @"";
int i=0;
for (i=0; i < [ingredients count]; i++) {
Ingredient *ingredient = [ingredients objectAtIndex:i];
ingredientTitle = ingredient.name;
ingredientAmount = ingredient.amount;
ingredientAisle = ingredient.aisle;
increments = [increments stringByAppendingFormat:recipeTitle];
increments = [tableFirstLine stringByAppendingFormat:@"<tr><td>"];
increments = [increments stringByAppendingFormat:ingredientTitle];
increments = [increments stringByAppendingFormat:@"</td><td>"];
increments = [increments stringByAppendingFormat:ingredientAmount];
increments = [increments stringByAppendingFormat:@"</td><td>"];
increments = [increments stringByAppendingFormat:ingredientAisle];
increments = [increments stringByAppendingFormat:@"</td></tr>"];
if (i == ([ingredients count]-1)) {
//IF THIS IS THE LAST INGREDIENT, CLOSE THE TABLE
increments = [increments stringByAppendingFormat:@"</table>"];
}
}
NSLog(@"CODE:: %@", increments);
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
[composer setSubject:@"subject here"];
[composer setMessageBody:increments isHTML:YES];
[composer setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:composer animated:YES];
[composer release];
}else {
[composer release];
}
}
但话又说回来,它显示在表中只有一行。我在这里做错了什么?
答
怎么是这样的:
NSString *recipeTitle = [NSString stringWithFormat:@"<h5>Recipe name: %@ </h5>", recipe.name];
我可以把多个%@,然后去喜欢......,recipe.name,recipe.preptime,recipe.included?我会在同一行代码中附加三个字符串。这不会是坏... – Farini
是的,你可以。您也可以查看文档以获取更多生成字符串的方法。 – Odrakir
这很完美。谢谢你的启发 – Farini