Zeeshan Shaikh
Zeeshan Shaikh

Reputation: 13

how to concatenate two strings in iphone

In this Code here i am sending Code number and phone number seperatly, i have two textFiedls phone code number field, and phone number field, but i want to concatenate code number and phone number field and send it as full mobile number...

Please help me how to concatenate codenumber and phone number.

self.operatorLbl.text = self.operatorStr;

    self.codeLbl.text = self.codeStr;

    self.phoneNumLbl.text = self.phoneNumStr;

    self.balanceLbl.text = self.balanceStr;

[post orderConfirm: self.operatorLbl.text :self.codeNumLbl.tex :self.phoneNumLbl.text :self.balanceLbl.text];

I'm very very new in this program need your help

Upvotes: 0

Views: 368

Answers (3)

Rajneesh071
Rajneesh071

Reputation: 31091

Here is all way you can do with string

//1st Way
NSString *finalString = [NSString stringWithFormat:@"%@ %@",firstString,secondString];

//2nd Way
NSString *finalString = [firstString stringByAppendingFormat:@" %@",secondString];

//3rd way
NSArray *ary= [NSArray arrayWithObjects:firstString,secondString, nil];
NSString *finalString= [ary componentsJoinedByString:@" "];

Upvotes: 2

user2377290
user2377290

Reputation:

you can try this one

    NSString *string1, *string2, *result;

string1 = @"This is ";
string2 = @"my string.";

result = [result stringByAppendingString:string1];
result = [result stringByAppendingString:string2];
OR

result = [result stringByAppendingString:@"This is "];
result = [result stringByAppendingString:@"my string."];

If a = AAA and b = BBB then you will need to write

[a stringByAppendingString:b];

So in your case it will be [codeLbl.text stringByAppendingString:phoneNumLbl.text];

For more information about this method please see NSString Documentation

If its not helping you please let me know

Upvotes: 1

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

You can append in different way like this...

First Way

NSString *combinedStr = [NSString stringWithFormat:@"%@ %@", firstStr, secondStr];

Second way

For Immutable String

NSString *firstStr = @"FirstString";
NSString *secondStr = @"SecondString";
NSString *concatinatedString = [firstStr stringByAppendingString:secondStr];

For a Mutable string:

NSMutableString *firstStr = [NSMutableString stringWithString:@"FirstString"];
NSString *secondStr = @"SecondString";
[firstStr appendString:secondStr];

Upvotes: 1

Related Questions