开发者

How to combine two strings in Objective-C for an iPhone app

开发者 https://www.devze.com 2023-03-25 04:03 出处:网络
How can I combine \"stringURL\" and \"stringSearch\" together? - (IBAction)search:(id)sender;{ stringURL = @\"http://www.websitehere.com/index.php?s=\";

How can I combine "stringURL" and "stringSearch" together?

- (IBAction)search:(id)sender;{
stringURL = @"http://www.websitehere.com/index.php?s=";
stringSearch = sea开发者_如何学Pythonrch.text;
/* Something such as:
 stringURL_ = stringURL + stringSearch */
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:stringURL_]]];
}


Philippe gave a good example.

You can also use plain stringWithFormat: method.

NSString *combined = [NSString stringWithFormat:@"%@%@", stringURL, stringSearch];

This way you can manipulate string even more by putting somethig inbetween the strings like:

NSString *combined = [NSString stringWithFormat:@"%@/someMethod.php?%@", stringURL, stringSearch];


NSString* combinedString = [stringUrl stringByAppendingString: search.text];


NSString * combined = [stringURL stringByAppendingString:stringSearch];


Instead of stringByAppendingString:, you could also use

NSString *combined = [NSString stringWithFormat: @"%@%@", 
                                 stringURL, stringSearch];

This is especially interesting/convenient if you have more than one string to append. Otherwise, the stringbyAppendingString: method is probably the better choice.


You can use stringByAppendingString:

 stringURL = [@"http://www.websitehere.com/index.php?s=" 
                      stringByAppendingString:search.text];


If you want to have some control about the format of the parameter you should assemble your URL string with

[NSString stringWithFormat:@"http://www.websitehere.com/index.php?s=%@", search.text]

This solution is charming because you can append almost anything which can be inserted into a printf-style format.


I would not have given the answer of such general question. There are many answers of same type question have already given. First find the answer of your question from existing question.

NSString* myURLString = [NSString stringWithFormat:@"http://www.websitehere.com/index.php?s=%@", search.text];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号