Concatenate String in String Objective-c - 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

Related

Include a variable in the middle of NSString

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)"

Dynamically change number of variables in NSStringWithFormat

Let's say I have string like this:
NSString *myString = [NSString stringWithFormat:#"%#%#%#%#%#",variable1,variable2,variable3,variable4,variable5];
and if variable2 is nil I don't want that in my string, like this:
NSString *myString = [NSString stringWithFormat:#"%#%#%#%#",variable1,variable3,variable4,variable5];
Question
Is there any way to do this without having a lot of if-statements?
Don't use [NSString stringWithFormat:], and instead create an NSMutableString and append strings as necessary:
NSMutableString *s = [[NSMutableString alloc] init];
if (variable1)
[s appendString:variable1];
if (variable2)
[s appendString:variable2];
if (variable3)
[s appendString:variable3];
if (variable4)
[s appendString:variable4];
if (variable5)
[s appendString:variable5];
(sorry I missed your point about "not having lots of if statements", however I don't think it can be avoided).
The easiest way is to use the compacted ternary operator to replace a null with an empty string. Using a slightly abridged version of your example:
NSString *myString = [NSString stringWithFormat:#"%#%#",variable1 ?: #"",variable2 ?: #""];

Format string that already has format specifier %#

I have a string which already contains a formatter %#.
NSString *str = #"This is an %#";
I need to parse that string and to replace %# with 'example'. If I use
[NSString stringWithFormat:#"%#", str];
I get the following output:
This is an %#
I want output like:
This is an example
NSString *str = #"This is an %#";
str = [str stringByReplacingOccurrencesOfString:#"%#" withString:#"example"];
I would recommand to use the formatted string as "format"
NSString *str = #"This is an %#";
str = [NSString stringWithFormat:str, #"example"];
is working with every type. A better solution than replacing, because you can use unspecified replacings
is very usefull if you use localized.strings with x values you want to add ;)

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 ];

Can we compare the NSString with NSMutableString

for example...
NSString string1 = #"Hello world";
NSMutableString string2 = [NSMutableString stringWithString: string1];
then... then can we compare these using following statement..? or there is any other way?
if(string1 isEqualToString:string2)
help me out please...
yes of course. an NSMutableString is an NSString, so your code is perfectly correct, except for some syntax errors (you missed the * on each NSString and the [ ] on the if statement. You should write :
NSString *string1 = #"Hello world";
NSMutableString *string2 = [NSMutableString stringWithString:string1];
if ([string1 isEqualToString:string2])
{
// string are equal
}