How-To Use iOS NSString Format Specifiers

Source

NSString’s string format specifiers are placeholders for data values of string or other data types like int, float, double, short, char and strings. The specifiers conform to IEEE printf specification. This guide provides a listing of the supported NSString string format specifiers and examples on where and how to use them in your Objective-C code. The NSString format specifiers in iOS are only a subset of the specifiers in the Cocoa SDK for Mac.

The NSString string format specifiers uses the the printf function in C to output the value as a string format. Although you don’t explicitly use the printf to output the string value, you still can. Instead, you use the NSLog to write to the stdout or to the console. You would also use the string specifiers and converters to assign a value of a certain type to a NSString variable or CFString variable. The printf is a function in the standard C library as is the scanf function which is used to read in data. In addition to the examples in this guide, the printf specification also contains explains how to use the escape character “\” but this is not covered in this guide.

%@
Writes an Objective-C string.
%%
Writes a % character.
%d,%D
Writes a signed 32-bit integer which is an int.
%u or %U
Writes an unsigned 32-bit integer which is an unsigned int
%x
Use this specifier to write signed int as a hexadecimal character using 0-9 digits and lowercase a-f characters.
%X
Use this specifier to write an unsigned int as a hexadecimal character using 0-9 digits and uppercase A-F characters.
%o,%O
The preceding specifier will write an unsigned 32-bit int in octal format.
%f
Use the preceding specifier to write a double value (64-bit floating-point number)
%e
To write a 64-bit floating-point double value in scientific notation (e or exponent format).
%E
To write a 64-bit floating-point double value in uppercase scientific notation (E or exponent format).
%g
To write out a double (64 bit floating point) in the %e format when the exponent is less than -4 or if greater or equal to the precision as %f.
%G
As the %g format but in uppercase (E).
%c
Output an unsigned char to the NSLog as ASCII or as an octal \\ddd format or in an unicode hexadecimal format of \\uddd when d is a digit.
%C
As above but using an unsigned 16-bit unichar character.
%s
Outputs an 8 bit null terminated unsigned character. The %s specifier interpretes characters using system default encodings and can cause uncertain results with certain languages like the right-left languages. For this reason it is best to use specific encodings.
%S
Writes out 16-bit Unicode null terminted characters.
%p
Print a void* pointer in hexadecimal format using digits ranging from 0-9 and lowercase characters between a-f inclusively with a leading 0x.
%a
%a specifier provides a character placeholder for a double also known as a 64-bit floating-point number. The value is written with a leading 0x in scientific with one hexadecimal digit before the decimal point using a lowercase p symbolto introduce the exponent.
%A
as above but the exponential symbol is in uppercase.
%F
Writes a double in decimal notation

Test String Specifier Code

-(void)writeToNSLog{
    /*
     @ to assign a string value to a variable or NSlog, append the @symbol to the string value.
     The following code snippet declares an int variable, then assign the value t a string variable using the %d format specifier. Finally the string variable is passed to the NSlog using the %@ format specifier.
     %d is used to write out an int
     */
    int x = 666;
    NSLog(@"The value of x is %d",x);
    
    
    NSString *xValue = [[NSString alloc]initWithFormat:@"This is the value of x: %d",x];
    
    NSLog(@"%@",xValue);
    
    NSLog(@"Print a percentage sign %%");
    
    /*unsigned integer %u, %U*/
    int udigit = -1;
    NSLog(@"Write an unsigned integer: %u", udigit);
    
    /*Write an int as a hexadecimal in lowercase and uppercase*/
    NSLog(@"Write an int as a hexadecimal value: %X", x);
    NSLog(@"Write an int as a hexadecimal value: %X", x);
    
    /*Write an int in octal format*/
    NSLog(@"Write an int as a hexadecimal value: %o", x);
    
    /*Write a double*/
    double d = 123.9844;
    NSLog(@"Write a double value: %f", d);
    NSLog(@"Write a double value in decimal: %F", d);
    
    /*Write a double in scientific notation*/
    NSLog(@"Write a double value in scientific notation: %e", d);
    
    /*Write a double in scientific notation*/
    NSLog(@"Write a double value in scientific notation: %g", d);
    
    /*Write a char*/
    char chr = 'a';
    char ddd= 0x0ddd;
     NSLog(@"Write a char: %c", chr);
     NSLog(@"Write a char: %c", ddd);
    
    /*write a char pointer*/
    char *nothing = "test";
    NSLog(@"Write a char: %s", nothing);
    NSLog(@"Write a pointer: %p", nothing);
    
    /*print a double in hexadecimal*/
    double dp = 123.321;
    NSLog(@"Write a double in hexadecimal format: %a", dp);
    
    
    
    
    
}

Console Output : stdout

2012-09-25 20:50:24.007 StringSpecifiers[4873:f803] The value of x is 666
2012-09-25 20:50:24.008 StringSpecifiers[4873:f803] This is the value of x: 666
2012-09-25 20:50:24.009 StringSpecifiers[4873:f803] Print a percentage sign %
2012-09-25 20:50:24.010 StringSpecifiers[4873:f803] Write an unsigned integer: 4294967295
2012-09-25 20:50:24.011 StringSpecifiers[4873:f803] Write an int as a hexadecimal value: 29A
2012-09-25 20:50:24.012 StringSpecifiers[4873:f803] Write an int as a hexadecimal value: 29A
2012-09-25 20:50:24.014 StringSpecifiers[4873:f803] Write an int as a hexadecimal value: 1232
2012-09-25 20:50:24.015 StringSpecifiers[4873:f803] Write a double value: 123.984400
2012-09-25 20:50:24.016 StringSpecifiers[4873:f803] Write a double value in decimal: 123.984400
2012-09-25 20:50:24.017 StringSpecifiers[4873:f803] Write a double value in scientific notation: 1.239844e+02
2012-09-25 20:50:24.018 StringSpecifiers[4873:f803] Write a double value in scientific notation: 123.984
2012-09-25 20:50:24.019 StringSpecifiers[4873:f803] Write a char: a
2012-09-25 20:50:24.020 StringSpecifiers[4873:f803] Write a char: ›
2012-09-25 20:50:24.021 StringSpecifiers[4873:f803] Write a char: test
2012-09-25 20:50:24.022 StringSpecifiers[4873:f803] Write a pointer: 0x3867
2012-09-25 20:50:24.023 StringSpecifiers[4873:f803] Write a double in hexadecimal format: 0x1.ed48b43958106p+6

Length Modifiers

Length modifiers are flags that allow you to define the length of an input variable that is very long or very short. You use these modifiers with the format specifiers. The general syntax example is:

NSLog(@”Write something about %d", 555555 );

Remember length modifiers are for input specifications. The following table provides a guide the available length modifiers and how to use them.

Modifier
Description
h
Use the h Length modifier to specify that a following d, o, u, x, or X conversion specifier will be a short or an unsigned short. applies to a signbed or unsigned short.
hh
For a signed or unsigned char, use the hh length modifier with d, o, u, x, or X conversion specifier.
l
To use a signed or unsigned long length modifier with one of the following d, o, u, x, or X conversion specifier
ll,q
Length modifiers for either a signed or unsigned long long for use with one of the following d, o, u, x, or X conversion specifier.
L
To convert a value to a long double with one of the following a, A, e, E, f, F, g, G conversion specifier.
z
For size_t of the signed integer type that is associated with the size_t values use the t tength modifier specifying that a following d, o, u, x, or X conversion specifier.
t
The length modifier for a ptrdiff_t and the associated unsigned int to be specified with the following d, o, u, x, or X conversion specifier.
j
The j length modifier works with the d, o, u, x, or X conversion specifiesr when working with intmax_t or uintmax_t argument.
-(void)lengthSpecifiers{
    /* Using the h length specifier*/
    short number = 9;
    NSLog(@"This is an int formated as a short %hd",number);
    
    char c = 'a';
    NSLog(@"The int value of a char: %hhd",c);
    
    long longOctal = 12345678;
    NSLog(@"A long in octal format %lo",longOctal);
    
    long long longLong = 999999999;
    NSLog(@"A long long %llo",longLong);
    
    long double longToDouble = 2223333;
    NSLog(@"A long value as a double %Le", longToDouble);
    
    size_t val = 5;
    NSLog(@"size_t value  %zd",val);
    
    ptrdiff_t pt=6;
    NSLog(@"ptrdiff_t value : %tx",pt);
    
    intmax_t scanVal = 33;
    NSLog(@"intmax_t value: %jd", scanVal);
}

Length Conversion Specifiers Output

2012-09-25 22:34:18.756 StringSpecifiers[5300:f803] This is an int formated as a short 9
2012-09-25 22:34:18.758 StringSpecifiers[5300:f803] The int value of a char: 97
2012-09-25 22:34:18.760 StringSpecifiers[5300:f803] A long in octal format 57060516
2012-09-25 22:34:18.760 StringSpecifiers[5300:f803] A long long 7346544777
2012-09-25 22:34:18.761 StringSpecifiers[5300:f803] A long value as a double 2.223333e+06
2012-09-25 22:34:18.761 StringSpecifiers[5300:f803] size_t value 5
2012-09-25 22:34:18.767 StringSpecifiers[5300:f803] ptrdiff_t value : 6
2012-09-25 22:34:18.768 StringSpecifiers[5300:f803] intmax_t value: 33

Platform Dependencies

Objective-C, through iOS and Cocoa Touch, has the APIs to elegantly represent 32 and 64 bit, numbers, like the signed and unsigned int and long, using the NSInteger and NSUinteger classes. Likewise the CGFloat class represents floating point numerical values on 32 and 64 bit systems instead of using the float or double. On the other hand, the CFIndex is a Core Foundation class of type signed long which has a higher degree of accuracy on 64 bit systems.

The following table list the specifiers to use with the different numerical classes and to perform casts.

Data Type
Specifier
Description
NSInteger
%ld, %lx
Cast a numerical value to the long data type
NSSUinteger
%lu, %lux
As above but for unsigned long data type.
CGFloat
%f, %g
Use the %f with floats or doubles when you need to format a numerical value of that type, but for scanning you will need to use the %f for floats and %ld for doubles. To use the scanf with a CGFloat, then use the double specifier and copy the result to a CGFloat, See the example below.
CFIndex
%ld, %lx
As the Nsinteger above.
pointer
%p and %zx
The %p specifier will append an 0x to a numerical output, otherwise use the %zx and no typecast if you rather not have the 0x at the beginnning.

Platform Dependencies

-(void)castingValues{
    NSInteger inVal = NSIntegerMax;
    NSLog(@" Cast a NSInteger to long %ld", (long)inVal);
    
    CGFloat cgfloat = 34.12;
    NSLog(@"Cast to float %f", (float)cgfloat);
    
    CFIndex index = 1;
    NSLog(@"Cast CFIndex to int %lx", (long)index);
    
    NSNumber *p = [[NSNumber alloc] initWithDouble:123.55];
    NSLog(@"Cast a pointer to hexadecimal %p", p);
    
    
    
}

2012-09-25 23:07:34.909 StringSpecifiers[5490:f803] Cast a NSInteger to long 2147483647
2012-09-25 23:07:34.910 StringSpecifiers[5490:f803] Cast to float 34.119999
2012-09-25 23:07:34.911 StringSpecifiers[5490:f803] Cast CFIndex to int 1
2012-09-25 23:07:34.912 StringSpecifiers[5490:f803] Cast a pointer to hexadecimal 0x68b0f10

More by this Author


Comments 2 comments

KDuBarry03 4 years ago

I won't lie, but Computer science and the language involved with it was always Greek to me; however, you definitely allowed me to have some more understanding through your detailed article. Keep it going, Klanguedoc! voted up and tweeted!


klanguedoc profile image

klanguedoc 4 years ago from Canada Author

Wow thanks Keith on your super feedback. I know a lot of my hubs are obscure to the average reader but I am trying to create a destination for mobile developers. If I write something that is understandable to the non-programmer, I really feel great.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    klanguedoc profile image

    Kevin Languedoc (klanguedoc)323 Followers
    144 Articles

    Kevin is Software Developer with 20 years experience designing and building software applications including iOS and Android apps.


    Click to Rate This Article
    working