PDA

View Full Version : Simple String Combining




thisma
Sep 9, 2008, 06:39 PM
I want to put two strings together to form the name of the file passed to a UIImage. I think something like this:


NSMutableString *tempName = [NSMutableString stringWithString:@"foo"];
[tempStemName appendString:@"bar.png"];
imageOfStem = [UIImage imageNamed:tempName];


will work.

My question is: Is there some way of doing this more simply or without so much juggling; this solution seems very bloated.

I know I can't do this:


imageOfStem = [UIImage imageNamed:@"foo" + @"bar.png"];


But, something like:


imageOfStem = [UIImage imageNamed:[@"foo" returnStringWithStringAppended:@"bar.png"];


would be good.

trick here is @"foo" needs to remain @"foo"

Sorry to be such a n00b; I'm working on it. I did spend the prerequisite time Googling and found my self more confused afterward.

Please help,
-thisma



AaRdVarK
Sep 9, 2008, 06:52 PM
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingString:

Luke Redpath
Sep 9, 2008, 07:05 PM
Look at NSString +stringWithFormat.

kainjow
Sep 9, 2008, 07:07 PM
FYI... use ... instead of <code> ... </code>

PhoneyDeveloper
Sep 9, 2008, 07:26 PM
This will work:

imageOfStem = [UIImage imageNamed:[@"foo" stringByAppendingString:@"bar.png"]];
imageOfStem = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@" @"foo", @"bar.png"]];

Working with immutable strings sometimes seems odd vs other languages that have a string concatenation operator.

thisma
Sep 9, 2008, 07:33 PM
Look at NSString +stringWithFormat.

interesting...
thank you

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingString:

Thank you. I'll look at that more tomorrow.

FYI... use ... instead of <code> ... </code>

:o also, thank you. :) I fixed it.

thisma
Sep 10, 2008, 01:17 AM
This will work:

imageOfStem = [UIImage imageNamed:[@"foo" stringByAppendingString:@"bar.png"]];
imageOfStem = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@" @"foo", @"bar.png"]];

Awesome :D thank you. That's what I was looking for.