本文介绍了Objective-C 中连接 NSString 的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Objective-C 中是否有 (stringByAppendingString:
) 字符串连接的快捷方式,或者通常使用 NSString
的快捷方式?
Are there any shortcuts to (stringByAppendingString:
) string concatenation in Objective-C, or shortcuts for working with NSString
in general?
例如,我想做:
NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];
更像是:
string myString = "This";
string test = myString + " is just a test";
推荐答案
我能想到的两个答案... 没有一个比使用串联运算符特别愉快.
Two answers I can think of... neither is particularly as pleasant as just having a concatenation operator.
首先,使用 NSMutableString
,它有一个 appendString
方法,消除了对额外临时字符串的一些需要.
First, use an NSMutableString
, which has an appendString
method, removing some of the need for extra temp strings.
其次,使用 NSArray
通过 componentsJoinedByString
方法进行连接.
Second, use an NSArray
to concatenate via the componentsJoinedByString
method.
这篇关于Objective-C 中连接 NSString 的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!