FreelancerJ
FreelancerJ

Reputation: 55

NSString and NSMutableString concatenation

I have three strings (a NSString, a NSMutableString, and another NSString) which I need to concatenate into a mutable string, in that order, to display as the source for a UIWebView. Comming from a PHP/JavaScript/HTML background, my knowledge of concatenation is pretty much this:

var concatenatedString = string1 + string2 + string3;

I presume that sort of thing won't work in Objective-C, so I'm wondering how to go about pulling them all together properly.

To give a bit of setting for this, the first string (NSString) is the header and canvas element of a web page, the second string (NSMutableString) is javascript from a text field that the user can define to manipulate the canvas element, and the third string (NSString) is the end tags of the web page.

Also, rather than initially creating the NSMutableString, should I just referance the UITextView.text to the get the user's text when concatenating the whole thing, or should I pull the text from the UITextView first?

Upvotes: 2

Views: 4307

Answers (3)

NJones
NJones

Reputation: 27147

The other two answers are correct in that they answer the question as you asked it. But by your description of what you want to do there is a much easier way. Use a format.

Assuming string1 and string3 will always be the same and only string2 will change,which is what it sounds like you are doing you can write something like this.

static NSString *formatString = @"String1Text%@String3Text";
NSString *completeString = [NSString stringWithFormat:formatString,self.myTextFieldName.text];
NSLog(@"%@",completeString);

The %@ in the format says to insert the description of the object following the format.(The description of an NSString is the stringValue.) Assuming you have a UITextField named myTextFieldName, that currently contains the text 'String2Text' Then this will be the output:

'String1TextString2TextString3Text'

In this way you only create 1 instance of an NSString format for the whole class no matter how many times you call this code.

To me it sounds like you don't need a mutable string at all. Feel free to leave a comment if I misunderstood anything.

Response to comment:

I'm not sure how you are implementing 'moves to test it out again' but, let's say you have a button named 'testJavaScript'. The IBAction method connected to that button would have the first two lines in it. So each time you pushed the button it would make a new formatted NSString filled with the current contents of the textfield. Once this string was formed it could not be changed. But it won't matter since next time it will make another.

Upvotes: 3

Jason
Jason

Reputation: 11832

NSString *concatenatedString = [string1 stringByAppendingFormat:@"%@%@", string2, string3];

You can make the resulting string mutable (if you really need to) by adding mutableCopy as shown in the answer by @Vinnie.

Upvotes: 1

Vinnie
Vinnie

Reputation: 1740

NSMutableString *concatenatedString = [[NSString stringWithFormat:@"%@%@%@", string1, string2, string3] mutableCopy];

Upvotes: 6

Related Questions