如何在iphone中连接两个字符串

时间:2013-05-16 05:23:50

标签: iphone string concatenation

在此代码中,我将分别发送代码和电话号码,我有两个textFiedls电话号码字段和电话号码字段,但我想连接代码和电话号码字段并将其作为完整的手机号码发送。 ..

请帮我解决如何连接号码和电话号码。

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

我在这个程序中非常新,需要你的帮助

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

你可以用不同的方式追加......

第一种方式

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

第二种方式

对于不可变的字符串

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

对于Mutable字符串:

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

答案 2 :(得分:1)

你可以尝试这个

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

如果a = AAA且b = BBB,则需要编写

[a stringByAppendingString:b];

因此,在您的情况下,它将是[codeLbl.text stringByAppendingString:phoneNumLbl.text];

有关此方法的详细信息,请参阅NSString Documentation

如果它没有帮助你,请告诉我