一个字符串中的多个变量

时间:2012-04-09 11:34:48

标签: objective-c nsstring

我目前有这段代码:

NSString *strURL = [NSString stringWithFormat:@"http://www.sample.com   /phpFile.php?firstname=%@",txtfirstName.text];

NSString *strURL2 = [NSString stringWithFormat:@"http://www.sample.com/phpFile.php?lastname=%@",txtlastName.text];

但问题是这会创建两个不同的行,我想要名字,姓氏和其他所有行。

所以可以这样做:

NSString *strURL = [NSString stringWithFormat:@"http://www.sample.com   /phpfile.php?firstname=%@",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];

还是我需要做一些更复杂的事情?

提前致谢!

3 个答案:

答案 0 :(得分:11)

NSString *strURL = [NSString stringWithFormat:@"http://www.sample.com/phpfile.php?firstname=%@&txtlastName=%@&txtheight=%@&txtweight=%@",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];

使用它并且喜欢这个

答案 1 :(得分:1)

NSString *strURL = [@"http://www.sample.com/phpfile.php" appendStringWithFormat:@"?firstname=%@&txtlastName=%@&txtheight=%@&txtweight=%@",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];

答案 2 :(得分:1)

试试这段代码:

NSString *subStringURL = [NSString stringWithFormat:@"firstname=%@&txtlastName=%@&txtheight=%@&txtweight=%@",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];

NSString *strURL = [NSString stringWithFormat:@"http://www.sample.com/phpfile.php?%@",subStringURL];

如果您需要更多帮助,请参阅此链接:stringWithFormat: method of NSString Class

希望这会对你有所帮助。