附加带有变量的字符串

发布于 2024-11-29 16:56:49 字数 483 浏览 1 评论 0原文

我是一个即将转向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风筝在阴天搁浅。 2024-12-06 16:56:49

您可以使用 appendString:,但一般来说,我更喜欢:

NSString *someText = [NSString stringWithFormat: @"Lorem ipsum %@", someMutableString];
NSString *someString = [NSString stringWithFormat: @"This is string is equal to %d.", someInt];
NSString *someOtherString = [NSString stringWithFormat: @"This is string is equal to %@.", someNSNumber];

或者,或者:

NSString *someOtherString = [NSString stringWithFormat: @"This is string is equal to %d.", [someNSNumber intValue]];

等等...

这些字符串是自动释放的,因此请注意不要丢失它们的值。如有必要,保留或复制它们并稍后自行释放。

You can use appendString:, but in general, I prefer:

NSString *someText = [NSString stringWithFormat: @"Lorem ipsum %@", someMutableString];
NSString *someString = [NSString stringWithFormat: @"This is string is equal to %d.", someInt];
NSString *someOtherString = [NSString stringWithFormat: @"This is string is equal to %@.", someNSNumber];

or, alternatively:

NSString *someOtherString = [NSString stringWithFormat: @"This is string is equal to %d.", [someNSNumber intValue]];

etc...

These strings are autoreleased, so take care not to lose their value. If necessary, retain or copy them and release them yourself later.

青衫负雪 2024-12-06 16:56:49

试试这个:

NSMutableString * string1 = [[NSMutableString alloc] initWithString:@"this is my string"];

[string1 appendString:@" with more strings attached"];

//release when done
[string1 release];

Try this:

NSMutableString * string1 = [[NSMutableString alloc] initWithString:@"this is my string"];

[string1 appendString:@" with more strings attached"];

//release when done
[string1 release];
揪着可爱 2024-12-06 16:56:49

您需要使用 stringByAppendingString

NSString* string = [[NSString alloc] initWithString:@"some string"];
string = [string stringByAppendingString:@" Sweet!"];

当然,完成后不要忘记 [string release];

You need to use stringByAppendingString

NSString* string = [[NSString alloc] initWithString:@"some string"];
string = [string stringByAppendingString:@" Sweet!"];

Don't forget to [string release]; when your done of course.

泪之魂 2024-12-06 16:56:49
NSMutableString *string = [[NSMutableString alloc] init];

[string appendFormat:@"more text %@", object ];
NSMutableString *string = [[NSMutableString alloc] init];

[string appendFormat:@"more text %@", object ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文