Adding ":" between two words in stringByAppendingString - objective-c

I need to add : between 2 strings when i add them together.
I couldnt find simple way to do that,and i am sure there is .
NSString *inBase64;
inBase64=[userName.text stringByAppendingString:idNumber.text];
I need: userName:idNumber
Thnks

You can use stringWithFormat...
NSString *inBase64 = [NSString stringWithFormat:#"%#:%#", userName.text, idNumber.text];

Another way. Useful when you have a lot of string parts:
NSArray *parts = #[ userName.text, idNumber.text, /* more */ ];
NSString *string = [parts componentsJoinedByString:#":"];

instead of stringByAppendingString: use stringByAppendingFormat:, like this:
inBase64 = [userName.text stringByAppendingFormat:#":%#", idNumber.text];
you can also use the [NSString stringWithFormat:] method, like this:
inBase64 = [NSString stringWithFormat:#"%#:%#", userName.text, idNumber.text];

Related

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 ?: #""];

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

Multiple variables in one string

I currently have this piece of code:
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com /phpFile.php?firstname=%#",txtfirstName.text];
NSString *strURL2 = [NSString stringWithFormat:#"http://www.sample.com/phpFile.php?lastname=%#",txtlastName.text];
But the problem is that this creates two different rows and I want the firstname, last name, and everything else on one row.
So would it be possible to do something like:
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com /phpfile.php?firstname=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
or do I need to do something more complicated?
Thanks in advance!
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com/phpfile.php?firstname=%#&txtlastName=%#&txtheight=%#&txtweight=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
use it and like this
NSString *strURL = [#"http://www.sample.com/phpfile.php" appendStringWithFormat:#"?firstname=%#&txtlastName=%#&txtheight=%#&txtweight=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
Try this code:
NSString *subStringURL = [NSString stringWithFormat:#"firstname=%#&txtlastName=%#&txtheight=%#&txtweight=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com/phpfile.php?%#",subStringURL];
Refer to this link if you need more help: stringWithFormat: method of NSString Class
Hope this helps you.

How to write a NSString array object in one go?

My problem is that I know NSString may used as an array and in my code I wrote it as such:
NSString *loadBackground[3];
loadBackground[0] = #"background1";
loadBackground[1] = #"background2";
loadBackground[2] = #"background3";
Is it possible to write the same code in one line instead of 4 lines?
Please use NSArray instead of c-arrays
NSArray *array = [NSArray arrayWithObjects:#"background1", #"background1" , #"background1", nil];
accessing a string:
NSString *aString = [array objectAtIndex:1]
Use an NSArray instead:
NSArray *bgs = [#"bg1,bg2,bg3" componentsSeparatedByString:#","]; // => [#"bg1", #"bg2", #"bg3"];
try this :
NSString *st11[3]={#"hai",#"hai1",#"hai2"};
NSLog(#"%#,%#,%#",st11[0],st11[1],st11[2]);
Not sure why you're not using NSArray, but if you really want to do it you can do:
NSString *loadBackground[3] = {#"background1", #"background2", #"background3"};
NSString *strs[3]={#"str1",#"str2",#"str3"};
use: strs[0];

NSObject to NSString Objective-C

Can someone help me to convert an NSObject to NSString?
I'm trying to do something like this -
NSString *address = [NSString stringWithFormat:ivpObj.addressStr];
But I got an warning - Format is not a string literal and no format arguments
Please some one help
How about this:
NSString *address = [NSString stringWithFormat:#"%#", ivpObj.addressStr];
Simpler still:
NSString *address = [ivpObj.addressStr description];
try this, it is worked for me
NSObject* obj= values[i];
NSString *StringObject= [NSString stringWithFormat:#"%#", obj];
it is giving you a warning because you are useing stringWithFormat and you are not passing in a format you just passing in either an NSString or a CString
so chose from on of the other options
[NSString stringWithString: ivpObj.addressStr]
[NSString stringWithCString: ivpObj.addressStr encoding: String_Encoding of you addressStr here]
[NSString stringWithUTF8String:ivpObj.addressStr]
this should remove your warnings, otherwise if you want to format the string using something similar to Eimantas's answer