codescracker


objective c

Objective-C Strings



« Previous Tutorial Next Tutorial »


In Objective-C language, a string is represented by using NSString and its subclass NSMutableString, provides several ways for creating string objects.

The simplest way to create a string object in Objective-C is to use the Objective-C @"..." construct like this:

NSString *greet = @"Hello Objective-C, I am string";

Objective-C String Example

Here is an example program which illustrates of creating and printing a string in Objective-C:

/* Objective-C Strings - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
   NSString *greet = @"Hello Objective-C, I am string";
   NSLog(@"Greeting message = %@\n", greet);
   return 0;
}

When the above code is compile and executed, it will produce the following output:

2014-08-03 13:18:37.932 demo[23926] Greeting message = Hello Objective-C, I am string

There are a wide range of methods in Objective-C for the manipulation of strings. These are listed here one by one.

(1) This returns the character at given array position

- (unichar)characterAtIndex:(NSUInteger)index;

(2) This returns capitalized representation of the receiver

- (NSString *)capitalizedString;

(3) This returns floating-point value of the receiver’s text as a float

- (float)floatValue;

(4) This returns floating-point value of the receiver’s text as a double

- (double)doubleValue;

(5) This returns the Boolean value, indicates whether a given string matches the beginning characters of the receiver or not

- (BOOL)hasPrefix:(NSString *)aString;

(6) This returns NSString object, initialized by using the given format string as a template into which the remaining argument values are substituted

- (id)initWithFormat:(NSString *)format ...;

(7) This returns the Boolean value that indicates whether the given string matches the ending characters of the receiver or not

- (BOOL)hasSuffix:(NSString *)aString;

(8) This returns NSInteger value of the receiver’s text

- (NSInteger)integerValue;

(9) This returns theBoolean value that indicates whether the given string is equal to the receiver using a literal Unicode-based comparison or not

- (BOOL)isEqualToString:(NSString *)aString;

(10) This returns the lower-cased representation of the receiver

- (NSString *)lowercaseString;

(11) This returns the number of Unicode characters in the receiver

- (NSUInteger)length;

(12) This finds and returns the range of the first occurrence of a given string within the receiver

- (NSRange)rangeOfString:(NSString *)aString;

(13) This returns string made by appending to the receiver, a string constructed from the given format string and the following arguments.

- (NSString *)stringByAppendingFormat:(NSString *)format ...;

(14) This returns new string containing the characters of the receiver from the one at a given index to the end

- (NSString *)substringFromIndex:(NSUInteger)anIndex;

(15) This returns new string made by removing from both the ends of receiver characters contained in the given character set

- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;

Example

Here is an example program

/* Objective-C Strings - Example Program */
		
#import <Foundation/Foundation.h>
int main ()
{
	NSString *strptr1 = @"Hello";
	NSString *strptr2 = @"World";
	NSString *strptr3;
	int  len;
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	/* an uppercase string */
	strptr3 = [strptr2 uppercaseString];
	NSLog(@"Uppercase String = %@\n", strptr3);
	
	/* will concatenates strptr1 and strptr2 */
	strptr3 = [strptr1 stringByAppendingFormat:@"World"];
	NSLog(@"Concatenated string = %@\n", strptr3);
	
	/* total length of strptr3 after concatenation */
	len = [strptr3 length];
	NSLog(@"Length of strptr3 = %d\n", len);
		
	/* InitWithFormat */
	strptr3 = [[NSString alloc] initWithFormat:@"%@ %@",strptr1,strptr2];	
	NSLog(@"Using initWithFormat = %@\n", strptr3);
	[pool drain];

	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-08-03 13:18:37.932 demo[30378] Uppercase String = WORLD
2014-08-03 13:18:37.932 demo[30378] Concatenated string = HelloWorld
2014-08-03 13:18:37.932 demo[30378] Length of strptr3 = 10
2014-08-03 13:18:37.932 demo[30378] Using initWithFormat = Hello World

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test