附加带有变量的字符串
我是一个即将转向 Objective-C 的 Java 人员。在 java 中,要将变量添加到字符串中,您必须执行以下操作:
someString = "This string is equal to " + someNumber + ".";
不过,我不知道如何在 Objective-C 中执行此操作。我有一个 NSMutableString
我想将其添加到字符串的中间。我该怎么做呢?
我尝试过:
NSString *someText = @"Lorem ipsum " + someMutableString;
NSString *someText = @"Lorem ipsum " + [someMutableString stringForm];
以及其他一些方法,但似乎都不起作用。还将 +
与 ,
互换。
I'm a java guy coming over to Objective-C. In java, to add a variable to a string you'd have to do something along the lines of:
someString = "This string is equal to " + someNumber + ".";
I can't figure out how to do it in Objective-C though. I have an NSMutableString
that I'd like to add to the middle of a string. How do I go about doing this?
I've tried:
NSString *someText = @"Lorem ipsum " + someMutableString;
NSString *someText = @"Lorem ipsum " + [someMutableString stringForm];
and a few other things, none of which seem to work. Also interchanged the +
s with ,
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
appendString:
,但一般来说,我更喜欢:或者,或者:
等等...
这些字符串是自动释放的,因此请注意不要丢失它们的值。如有必要,保留或复制它们并稍后自行释放。
You can use
appendString:
, but in general, I prefer:or, alternatively:
etc...
These strings are autoreleased, so take care not to lose their value. If necessary, retain or copy them and release them yourself later.
试试这个:
Try this:
您需要使用
stringByAppendingString
当然,完成后不要忘记
[string release];
。You need to use
stringByAppendingString
Don't forget to
[string release];
when your done of course.