Include a variable in the middle of NSString - objective-c

I currently tried to use this:
NSString *hello = #"Hello";
NSString *whatever = [hello stringByAppendingString:#", world!"];
it's possible to use this but it will make a lot of work because my goal is to do something like this:
NSString = "Hello" +variable+ "My name is" +variable + ",Good day"

You can use NSString's method stringWithFormat
example
NSString *str = [NSString stringWithFormat:#"Hello %# My Name is %# ",var1,var2]
or
NSString *str = [NSString stringWithFormat:#"Hello %# My Name is %# ",#"Stack Overflow",#"FreelancsAndroidLovesyou"]

You can do this alternatively, if both values are same then you can do this,
NSString *string = #"Hello YOUR_NAME, My name is YOUR_NAME, Good day";
string = [string stringByReplacingOccurrencesOfString:#"YOUR_NAME" withString:#"Your_desired_Name"];
UPDATE
If both values are different then you can do this,
NSString *string = #"Hello %#, My name is %#, Good day";
NSString *output = [NSString stringWithFormat:string, variable1, variable2];

You can use the NSString method stringWithFormat.
Objective C
NSString *strConcat = [NSString stringWithFormat:#"Hello %# My Name is %# ",var1,var2];
Swift
let strConcat = "Hello "+ strOne + " My Name is " + strTwo
OR
let strConcateSecond = "Hello \(strOne) My Name is \(strTwo)"

Related

regex for adding a space between two words if not already there

I would like to add a spacing between two words if its already not there:
//Sample 1
NSString *word1 = #"First";
NSString *word2 = #"Word";
NSString *output = [NSString stringWithFormat:#"%#%#", word1, word2];
//output = FirstWord --> I want "First Word"
If there is already a space "First " then it should not add another one.
Just trim the first string, and then always put a space.
NSString *word1 = #"First";
NSString *word2 = #"Word";
NSString *word1Trimmed = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
NSString *output = [NSString stringWithFormat:#"%# %#", word1Trimmed, word2];

Message body text + label.text

I want to create a message that has a string text and also reference a label's text too.
I have this, but I'm not sure how to tie it together.
NSString *message = #"Lets meet here:"; _addressLabel.text;
You can use stringWithFormat:
NSString *message = [NSString stringWithFormat:#"Lets meet here: %#", _addressLabel.text];
The %# tells the method where to substitute the argument - you can even have multiple:
NSString *foo = #"foo";
NSString *bar = #"bar";
NSString *message = [NSString stringWithFormat:#"%# : %#", foo, bar];
will make message be foo : bar.

Inject a character while concatenating with stringByAppendingString

Is there a way to concatenate two strings and inject a character. I have :
NSString *firstString = #"http://www.stackoverflow.com";
NSString *secondString = #"supercooloptions";
NSString *result = [firstString stringByAppendingString: secondString];
NSLog(#"the final string with "/" injection = %#", result);
I'd like the final string with "/" injected = www.stackoverflow.com/supercooloptions
You can use a different method - for example, stringWithFormat:
NSString *result = [NSString stringWithFormat:#"%#/%#", firstString, secondString];
For this specific case, use stringByAppendingPathComponent:
NSString *result = [firstString stringByAppendingPathComponent:secondString];

Concatenate String in String Objective-c

I want to place a string within a string. Basically in pseudo code:
"first part of string" + "(varying string)" + "third part of string"
How can I do this in objective-c? Is there a way to easily concatenate in obj-c? Thanks!
Yes, do
NSString *str = [NSString stringWithFormat: #"first part %# second part", varyingString];
For concatenation you can use stringByAppendingString
NSString *str = #"hello ";
str = [str stringByAppendingString:#"world"]; //str is now "hello world"
For multiple strings
NSString *varyingString1 = #"hello";
NSString *varyingString2 = #"world";
NSString *str = [NSString stringWithFormat: #"%# %#", varyingString1, varyingString2];
//str is now "hello world"
Variations on a theme:
NSString *varying = #"whatever it is";
NSString *final = [NSString stringWithFormat:#"first part %# third part", varying];
NSString *varying = #"whatever it is";
NSString *final = [[#"first part" stringByAppendingString:varying] stringByAppendingString:#"second part"];
NSMutableString *final = [NSMutableString stringWithString:#"first part"];
[final appendFormat:#"%# third part", varying];
NSMutableString *final = [NSMutableString stringWithString:#"first part"];
[final appendString:varying];
[final appendString:#"third part"];
NSString * varyingString = ...;
NSString * cat = [NSString stringWithFormat:#"%s%#%#",
"first part of string",
varyingString,
#"third part of string"];
or simply -[NSString stringByAppendingString:]
You would normally use -stringWithFormat here.
NSString *myString = [NSString stringWithFormat:#"%#%#%#", #"some text", stringVariable, #"some more text"];
Just do
NSString* newString=[NSString stringWithFormat:#"first part of string (%#) third part of string", #"foo"];
This gives you
#"first part of string (foo) third part of string"
Iam amazed that none of the top answers pointed out that under recent Objective-C versions (after they added literals), you can concatenate just like this:
#"first" #"second"
And it will result in:
#"firstsecond"
You can not use it with NSString objects, only with literals, but it can be useful in some cases.
simple one:
[[#"first" stringByAppendingString:#"second"] stringByAppendingString:#"third"];
if you have many STRINGS to Concatenate, you should use NSMutableString for better performance

Append string with variable

I'm a java guy coming over to Objective-C. In java, to add a variable to a string you'd have to do something along the lines of:
someString = "This string is equal to " + someNumber + ".";
I can't figure out how to do it in Objective-C though. I have an NSMutableString that I'd like to add to the middle of a string. How do I go about doing this?
I've tried:
NSString *someText = #"Lorem ipsum " + someMutableString;
NSString *someText = #"Lorem ipsum " + [someMutableString stringForm];
and a few other things, none of which seem to work. Also interchanged the +s with ,s.
You can use appendString:, but in general, I prefer:
NSString *someText = [NSString stringWithFormat: #"Lorem ipsum %#", someMutableString];
NSString *someString = [NSString stringWithFormat: #"This is string is equal to %d.", someInt];
NSString *someOtherString = [NSString stringWithFormat: #"This is string is equal to %#.", someNSNumber];
or, alternatively:
NSString *someOtherString = [NSString stringWithFormat: #"This is string is equal to %d.", [someNSNumber intValue]];
etc...
These strings are autoreleased, so take care not to lose their value. If necessary, retain or copy them and release them yourself later.
Try this:
NSMutableString * string1 = [[NSMutableString alloc] initWithString:#"this is my string"];
[string1 appendString:#" with more strings attached"];
//release when done
[string1 release];
You need to use stringByAppendingString
NSString* string = [[NSString alloc] initWithString:#"some string"];
string = [string stringByAppendingString:#" Sweet!"];
Don't forget to [string release]; when your done of course.
NSMutableString *string = [[NSMutableString alloc] init];
[string appendFormat:#"more text %#", object ];