how to concatenate two strings in iphone

不想你离开。 提交于 2019-12-13 04:24:26

问题


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


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/16579543/how-to-concatenate-two-strings-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!