Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

strings
objectivec
communitycreator

What are strings in Objective-C?

Fouzia Bashir

Overview

Strings in Objective-C are denoted by NSString. Its subclass NSMutableString offers multiple ways to create objects regarding strings.

Syntax

NSString *Name = @"Edpresso";

Let’s discuss an example to create a string object.

#import <Foundation/Foundation.h>
int main () {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSString *Name = @"John";
   NSLog(@"First Name: %@\n", Name );
   [pool drain];
   return 0;
}
  • Line 3: An NSAutoreleasePool interface reference is created.

Note: The NSAutoreleasePool interface implements an autorelease pool that stores objects that are drained before the end of program. It helps to deallocate and release memory of variables automatically without any external garbage collection.

  • Line 4: We create a string with the value John.
  • Line 5: We print the string to the console.
Methods for Basic String Operations

Method

Description

(NSString*)capitalizedString;

It returns the capital illustration of the string.

(Unichar)characterAtindex;

It returns the character at a specified array position.

(double)doubleValue;

It returns the decimal value of text as double.

(float)floatValue;

It reruns the decimal value of text as the float.

(BOOL)hasPrefix:(NSString*)aString;

It returns the Boolean value that shows whether the defined string matches the starting character of the receiver or not.

(BOOL)hasSuffix:(NSString*)aString;

It returns the Boolean value that shows whether the defined string matches the ending character of the receiver or not.

(id)initWithFormat:(NSString*)format…;

It returns the NSString object. It is initialized with the help of a specified format regarding string. 

(NSInteger)integerValue;

It returns the NSInteger of the text.

(BOOL)isEqualToString:(NSString*)aString;

It returns a Boolean value that shows whether the defined string is equal to the receiver or not. It is done by using a literal Unicode-based comparison.

(NSUInteger)length;

It returns the number of characters regarding Unicode in the text.

(NSString*)lowercaseString;

It returns the lowercased illustration of the text.

(NSString*)subStringFromIndex:(NSUInteger)anIndex;

It returns a new string.it contains the characters regarding receiver from the character at defined index o the last one.

(NSRange)rangeOfString:(NSString*)aString;

It returns the range of the first occurrence of the defined string in the receiver.

(NSString*)stringByAppendingFormat:(NSString*)format…;

It returns the string constructed by appending or joining a string made from the defined format string and the following parameters to the receiver.

(NSString*)stringByTrimmingCharacterInSet:(NSCharaterSet*)set;

It returns a new string. This string is constructed by removing from both ends of receiver characters confined in a present character set.

Explanation

In the code snippet below, we’ll use built-in string operations and methods.

#import <Foundation/Foundation.h>
/* String operations*/
int main () {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSString *s1 = @"Hi,";
   NSString *s2 = @"John";
   NSString *s3;
   int length;

   /* uppercased text or string  */
   s3 = [s2 uppercaseString];
   NSLog(@"Uppercase String :  %@\n", s3 );

   /* concatenating s1 and s2 */
   s3 = [s1 stringByAppendingFormat:@"John"];
   NSLog(@"The concatenated text:   %@\n", s3 );

   /* total length of s3 after the concatenation */
   length = [s3 length];
   NSLog(@"Length of S3 :  %d\n", length );

   /* InitWithFormat */
   s3 = [[NSString alloc] initWithFormat:@ "%@ %@", s1, s2];	
   NSLog(@"Using initWithFormat:   %@\n", s3 );

   [pool drain];
   return 0;
}
  • Line 4: We create an NSAutoreleasePool interface reference is created.
  • Line 5–8: We declare three strings and one variable to keep string length.
  • Line 11: We call uppercaseString to capitalize the string s2 to s3.
  • Line 15: We use stringByAppendingFormat to concatenate strings s1 and s2.
  • Line 19: We extract string s3 length to length variable.
  • Line 23: We use initWithFormat to show s1 and s2 in the specified format "%@ %@". We first print string s1, then put a space character, and finally print s2.

RELATED TAGS

strings
objectivec
communitycreator
RELATED COURSES

View all Courses

Keep Exploring