Header Ads

Strings - Objective C





The string in Objective C programming language is represented using NSString and its subclass NSMutableString provides ways for creating string objects. The simplest way to create a string object is to use the Objective C @".." construct:


NSString *greeting = @Hello";

A simple example for creating and printing a string is shown below:

#import <Foundation/Foundation.h>
int main()
{
       NSString *greeting = @Hello";
       NSLog(@"Greeting message: %@\n", greeting );
       return 0;
}

S.N. Method and purpose
1 -(NSString *)capitalizedString;
Returns a capitalized representation of the receiver.
2 -(unsigned)characterAtIndex : (NSUInteger)index;
Return the character at a given array position.
3
-(double)doubleValue;
Return the floating-point value the receiver's text as a double.
4
-(float)floatValue;
Return the floating-point value of the receiver's text as a float.
5
-(BOOL)hasPrefix : (NSString *)aString;
Return a boolean value that indicates whether a given string matches the begining characters of the receiver.
6
-(BOOL)hasSuffix : (NSString *)aString;
Return a boolean value that indicates whether a given string matches the ending characters of the receiver.
7
-(NSInteger)integerValue;
Return the NSInteger value of the reciver's text.
8
-(id)initWithFormat : (NSString *)format...;
Return an NSString object initialized by using a given format string as a template into which the remaining argument values are substituted.
9
-(BOOL)isEqualToString : (NSString *)aStringl
return a boolean value that indicate whether a given string is equal to the reciver using a literal unicode-based comparison.
10
-(NSUInteger)length;
Return the number of unicode character in the receiver.
11
-(NSString *)lowercaseString;
Return lowercae representation of the receiver/
12
-(NSRange)rangeOfString : (NSString *)aString;
Finds and return the range of the first occurence of a given string within the receiver.
13
-(NSString *)stringByAppendingFormat : (NSString *)foramt..;
 Return a string made by appending to the receiver a string constructed from a given format string and the following argument.
14
-(NSString *)stringByTrimmingCharactersinSet : (NSCharacterSet *)set;
Return a new string made by removing from both ends of the receive characters contained in a given character set.
15
-(NSString *)substringFromIndex : (NSUInteger)atIndex;
Return a new string containing the characters of the receiver from the one at a given index to the end.


Following example makes use of few of the above mentioned functions:


#import <Foundation/Foundation.h>
int main()
{
       NSString *str1 = @"Hello";
       NSString *str2 = @"World";
       NSString *str3;
      int len ;
     
      NSAutoreleasePool *pool = [[NSAutoreleasePool]alloc]init];
       
           //uppercase string
       str3  = [str2 uppercaseString];
       NSLog(@"Uppercase String: %@\n", str3 );

          //concatenates str1 and str2
      str3 = [str1 stringByAppedingFormat : @"World"];
      NSLog(@"Concatenated string: %@]n", str3);
 
        //total length of str3 after concatenation
      len = [str3 length];
      NSLog(@"Length of str3 : %d\n", len);
 
     //initwithFormat
    str3 = [[NSString alloc]initWithFormat:@"%@ %@",  str1, str2);
    NSLog(@"Using initwithformat : %@\n", str3);
    [pool drain];

    return 0;
}
       return 0;
}


No comments:

Powered by Blogger.