Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 600 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shortcuts in Objective-C to concatenate NSStrings

#11
Try stringWithFormat:

NSString *myString = [NSString stringWithFormat:@"%@ %@ %@ %d", "The", "Answer", "Is", 42];
Reply

#12
When dealing with strings often I find it easier to make the source file ObjC++, then I can concatenate std::strings using the second method shown in the question.

std::string stdstr = [nsstr UTF8String];

//easier to read and more portable string manipulation goes here...

NSString* nsstr = [NSString stringWithUTF8String:stdstr.c_str()];

Reply

#13
listOfCatalogIDs =[@[@"id[]=",listOfCatalogIDs] componentsJoinedByString:@""];
Reply

#14
NSString *result=[NSString stringWithFormat:@"%@ %@", @"Hello", @"World"];
Reply

#15
My preferred method is this:

NSString *firstString = @"foo";
NSString *secondString = @"bar";
NSString *thirdString = @"baz";

NSString *joinedString = [@[firstString, secondString, thirdString] join];

You can achieve it by adding the join method to NSArray with a category:

#import "NSArray+Join.h"
@implementation NSArray (Join)
-(NSString *)join
{
return [self componentsJoinedByString:@""];
}
@end

`@[]` it's the short definition for `NSArray`, I think this is the fastest method to concatenate strings.

If you don't want to use the category, use directly the `componentsJoinedByString:` method:

NSString *joinedString = [@[firstString, secondString, thirdString] componentsJoinedByString:@""];
Reply

#16
You can use NSArray as

NSString *string1=@"This"

NSString *string2=@"is just"

NSString *string3=@"a test"

NSArray *myStrings = [[NSArray alloc] initWithObjects:string1, string2, string3,nil];

NSString *fullLengthString = [myStrings componentsJoinedByString:@" "];

or

you can use

NSString *imageFullName=[NSString stringWithFormat:@"%@ %@ %@.", string1,string2,string3];
Reply

#17
**Macro:**

// stringConcat(...)
// A shortcut for concatenating strings (or objects' string representations).
// Input: Any number of non-nil NSObjects.
// Output: All arguments concatenated together into a single NSString.

#define stringConcat(...) \
[@[__VA_ARGS__] componentsJoinedByString:@""]



**Test Cases:**

- (void)testStringConcat {
NSString *actual;

actual = stringConcat(); //might not make sense, but it's still a valid expression.
STAssertEqualObjects(@"", actual, @"stringConcat");

actual = stringConcat(@"A");
STAssertEqualObjects(@"A", actual, @"stringConcat");

actual = stringConcat(@"A", @"B");
STAssertEqualObjects(@"AB", actual, @"stringConcat");

actual = stringConcat(@"A", @"B", @"C");
STAssertEqualObjects(@"ABC", actual, @"stringConcat");

// works on all NSObjects (not just strings):
actual = stringConcat(@1, @" ", @2, @" ", @3);
STAssertEqualObjects(@"1 2 3", actual, @"stringConcat");
}

---

**Alternate macro:** (if you wanted to enforce a minimum number of arguments)

// stringConcat(...)
// A shortcut for concatenating strings (or objects' string representations).
// Input: Two or more non-nil NSObjects.
// Output: All arguments concatenated together into a single NSString.

#define stringConcat(str1, str2, ...) \
[@[ str1, str2, ##__VA_ARGS__] componentsJoinedByString:@""];
Reply

#18
I tried this code. it's worked for me.

NSMutableString * myString=[[NSMutableString alloc]init];
myString=[myString stringByAppendingString:@"first value"];
myString=[myString stringByAppendingString:@"second string"];
Reply

#19
NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];

After a couple of years now with Objective C I think this is the best way to work with Objective C to achieve what you are trying to achieve.

Start keying in "N" in your Xcode application and it autocompletes to "NSString".
key in "str" and it autocompletes to "stringByAppendingString". So the keystrokes are quite limited.

Once you get the hang of hitting the "@" key and tabbing the process of writing readable code no longer becomes a problem. It is just a matter of adapting.
Reply

#20
If you have 2 NSString **literals**, you can also just do this:

NSString *joinedFromLiterals = @"ONE " @"MILLION " @"YEARS " @"DUNGEON!!!";

That's also useful for joining #defines:

#define STRINGA @"Also, I don't know "
#define STRINGB @"where food comes from."
#define JOINED STRINGA STRINGB

Enjoy.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through