Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

- (IBAction)search:(id)sender;{
stringURL = @"http://www.websitehere.com/index.php?s=";
stringSearch = search.text;
/* Something such as:
 stringURL_ = stringURL + stringSearch */
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:stringURL_]]];
}
like image 394
Adam U. Avatar asked Aug 04 '11 20:08

Adam U.


4 Answers

NSString* combinedString = [stringUrl stringByAppendingString: search.text];
like image 140
Nico Avatar answered Nov 15 '22 01:11

Nico


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];
like image 29
Cyprian Avatar answered Nov 15 '22 02:11

Cyprian


NSString * combined = [stringURL stringByAppendingString:stringSearch];

like image 6
Fabian Schuiki Avatar answered Nov 15 '22 01:11

Fabian Schuiki


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.

like image 5
Rudy Velthuis Avatar answered Nov 15 '22 01:11

Rudy Velthuis