Changes

Jump to: navigation, search

Working with String Objects in Objective-C

990 bytes added, 15:51, 4 November 2009
Converting Strings to Numbers
== Converting Strings to Numbers ==
 
String objects can be converted to a variety of number types:
 
* '''Convert String to int'''
 
<pre>
NSString *string1 = @"10";
 
int myInt = [string1 intValue];
 
NSLog (@"%i", myInt);
</pre>
 
* '''Convert String to double'''
 
<pre>
NSString *string1 = @"10.1092";
 
double myDouble = [string1 doubleValue];
 
NSLog (@"%f", myDouble);
</pre>
 
* '''Convert String to float'''
 
<pre>
NSString *string1 = @"10.1092";
 
float myFloat = [string1 floatValue];
 
NSLog (@"%f", myFloat);
</pre>
 
* '''Convert String to NSInteger'''
 
<pre>
NSString *string1 = @"10";
 
NSInteger myInteger = [string1 integerValue];
 
NSLog (@"%li", myInteger);
</pre>
 
== Converting a String Object to ASCII ==
 
The string contained within a string object can be extracted an converted to an ASCII C style character string using the ''UTF8String'' method. For example:
 
<pre>
NSString *string1 = @"The quick browen fox";
 
const char *utfString = [string1 UTF8String];
 
printf ("Converted string = %s\n", utfString);
</pre>

Navigation menu