14

I am a java programmer, I found that Java is very good at doing string.

If I want to do this objective c, how can I do in objective c ?

System.out.println("This is a " + 123 + " test");

2 Answers 2

37

To place an integer into a string, you can do this:

int n = 123;
NSString *s = [NSString stringWithFormat:@"This is a %d test", n];

There are numerous other ways. But concatenating strings with integers by + operator is not one of them. :)

Sign up to request clarification or add additional context in comments.

6 Comments

And for printing debugging output to the console, NSLog() is your friend.
As in, NSLog(CFSTR("This is a %d test"), n); The difference of syntax has to do with the fact that NSString is an Objective C class with methods, and NSLog is a C function.
@Seva that works, but it's much easier to do: NSLog(@"This is a %d test", n); Even though NSLog is a C function, it takes NSString*,... as its arguments.
You know... conceptual purity :) I'm pretending CFString and NSString are distinct, even though they're the same.
But you can have C functions that take objective C objects that don't have Core Foundation equivalents…
|
0

To place an integer into a string, you can do this:

int number = 123;
NSString *string = [NSString stringWithFormat:@"This is a %i test", number];

Or if you want to NSLog you have to do this :

int number = 123;
NSLog(@"This is a %i test", number);

It is very EASY !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.