NSString
NSString *s = @"Hello";
or
NSString *s;
s = @"Hello";
Assume
NSString *s=@"Hello";
NSString *s2=@"Bye";
NSString *s3=@"97";
NSString *s4=@"97.2";
int x = 2;
int y = 4;
double z = 5.768;
for all examples
Useful Static Methods:
stringWithFormat:aString, parameter(s)
Examples:
s=[NSString stringWithFormat:@"%i",x];
(s would be: 2)
s=[NSString stringWithFormat:@"The point is %i %i",x,y];
(s would be: The point is 2 4)
s=[NSString stringWithFormat:@"The answer is %.2f",z];
(s would be: The answer is 5.77)
format codes (for replacement variables or constants):
%c for a char
%i for an int
%f for a float or double
%@ for an NSString
stringWithString:aString
Example:
s=[NSString stringWithString:s2];
(s would be: Bye)
More Object Methods:
length
Example:
x = [s length];
(x would be 5, the number of characters in Hello)
characterAtIndex:anInt
Examples:
char ch = [s characterAtIndex:2];
(ch would be: l, as indexing of characters
start at 0)
char ch = [s characterAtIndex:1];
(ch would be: e)
char ch = [s characterAtIndex:0];
(ch would be: H)
stringByAppendingFormat:aString, vars
Examples:
s = [s stringByAppendingFormat:@"%i",x];
(s would be: Hello2)
s = [s stringByAppendingFormat:@" x = %i",x];
(s would be: Hello x = 2)
stringByAppendingString:aString
Example:
s = [s stringByAppendingString:@"bye"];
(s would be: Hellobye)
isEqualToString:aString
Example:
if ([s isEqualToString:s2]==YES)
intValue
Example:
x = [s3 intValue];
(x would be: 97)
doubleValue
Examples:
double z = [s3 doubleValue];
(z would be: 97.0)
double z = [s4 doubleValue];
(z would be: 97.2)
writeToFile: atomically: encoding: error:
Example: