Multiple variables in one string - objective-c

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.

Related

NSString get substring separed by 2 defined strings

I have some NSString object, for example:
NSString *inputString1 = #"Name is \"Mark\"";
NSString *inputString2 = #"Name is \"Joe\"";
I need to extract Mark and Joe values substrings from input.
Are there any solutions?
Thank you, in advance.
NSString *firstName =
[[inputString1 componentsSeparatedByString:#"\""] objectAtIndex:1];
NSString *name = [[inputString componentsSeparatedByString:#" "] lastObject];
name = [name stringByReplacingOccurencesOfString:#"\"" withString:#""];

Adding ":" between two words in stringByAppendingString

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

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

NSString: changing a filename but not the extension

Times like this and my Objective-C noobness shows. :-/
So, the more I work on a routine to do this, the more complex it's becoming, and I'm wondering if there isn't just a simple method to change the name of a filename in a path. Basically, I want to change #"/some/path/abc.txt to #"/some/path/xyz.txt -- replacing the filename portion but not changing the path or extension.
Thanks!
Try the following:
NSString* initPath = ...
NSString *newPath = [[NSString stringWithFormat:#"%#/%#",
[initPath stringByDeletingLastPathComponent], newFileName]
stringByAppendingPathExtension:[initPath pathExtension]];
What Vladimir said, just broken down more to make it a little easier to read:
NSString *pathToFile = #"/Path/To/File.txt";
NSString *oldFileName = [pathToFile lastPathComponent];
NSString *newFileName = [#"Document" stringByAppendingPathExtension:[oldFileName pathExtension];
NSString *newPathToFile = [pathToFile stringByDeletingLastPathComponent];
[newPathToFile stringByAppendingString:newFileName];
Take a look at the "Working With Paths" section of the NSString docs. In particular, lastPathComponent, pathExtension and stringByDeletingPathExtension: should do what you want.
You can try something like this:
NSRange slashRange = [myString rangeOfString:#"\\" options:NSBackwardsSearch];
NSRange periodRange = [myString rangeOfString:#"." options:NSBackwardsSearch];
NSString *newString = [myString stringByReplacingCharactersInRange:NSMakeRange(slashRange.location, periodRange.location) withString:#"replacement-string-here"];
What this code does is it gets the location of the \ and . characters and performs a backwards search so that it returns the last occurrence of it in the string.
Then, it creates a new range based on those previous ranges and replaces the contents in that range with stringByReplacingCharactersInRange:.
Try this:
NSString* path = [startString stringByDeletingLastPathComponent];
NSString* extension = [startString pathExtension];
NSString* replacementFileName = [#"foo" stringByAppendingPathExtension: extension];
NSString result = [path stringByAppendingPathComponent: replacementFileName];