关于iphone:在C中将字符串连接在一起

concatenate strings together in objective c

本问题已经有最佳答案,请猛点这里访问。

如何在目标C中连接字符串?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (IBAction)emailButton:(id)sender {
MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init];
[mailContoller setMailComposeDelegate:self];
NSString *email = @"*******gmail.com";
NSString *email1 = @"*******@hotmail.co.uk";
NSArray *emailArray = [[NSArray alloc]initWithObjects:email, email1, nil];
NSString *message = [NSString stringWithFormat:@"%@
%@
%@"
,
textField1.text, textField2.text, textField3.text];
[mailContoller setMessageBody:message isHTML:NO];
[mailContoller setToRecipients:emailArray];
[mailContoller setSubject:@"IT WORKS!"];
[self presentViewController:mailContoller animated:YES completion:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self myTextView] resignFirstResponder];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

如有任何帮助,我们将不胜感激。

谢谢您


在最简单的情况下:

1
NSString* concatenatedString = [stringA stringByAppendingString: stringB];

根据您的代码,这:

1
2
3
NSString *message = [NSString stringWithFormat:@"%@
%@
%@"
, textField1.text, textField2.text, textField3.text];

可以更好地表示为:

1
2
NSString *message = [@[textField1.text, textField2.text, textField3.text] componentsJoinedByString: @"
"
];

当只进行简单的连接时,+stringWithFormat:相对比较昂贵。