iPhone : Exploring the Foundation Framework – NS string class

NS string class:-

The NSString class and its mutable subclass, NSMutableString, provide an extensive set of APIS for working with strings, including methods for comparing, searching, and modifying strings. NSString objects are used extensively throughout Foundation and other Cocoa frameworks, serving as the basis for all textual and linguistic functionality on the platform. (To know further class types, refer to Exploring The Foundation Framework).

NSString is “toll-free bridged” with its Core Foundation counterpart, CFStringRef.


         NSString * str =  @  “hello”;
    Str  = [NSString     StringwithString :  @  “hello”];
    NSLog (@ “the string  is @”,   str);


Int  x  =  23;
Float  y  =  25;
Str  = [NSString    String with Format:  @  “%d++%0.of “’,x, y];
NSLog(@ “str is   %@”, str);

//Conversion string to normal data type:

NSString *str3 =[NSString StringwithString   : @ “9989645323];
Int  number  =  [str3 in value];
NSLog (@  “number is %d”, number”);

//length of an string:

Int length   =[Str length];

//Lower case and upper case:

NSString *uppercase   =[Str   upperCaseString];
Ns string  *lowercase  =  [Str  lowerCase string];

//string comparison:

If ([str is equal to string :str3])
   Ns log (@  “strings are equal”);
Ns log (@ “the string are not equal”);


  // substring
// hello world
// 012345678910

--> // substrings
  NSString *sub string    =   [Str SubStringFromIndex:1];
  NSString * to string =  [Str .SubStringToIndex  :5];
Ns log (@    “the SubString is %@;   SubString);
Ns log (@ “the to String is %@;    to String );

// String  Separation :

  NSString   *  str4   =  @”23-4-2011”;
  NSArray    *  a array=[str4 Component separated by string :@  “-“];
  NSLog (@  “a Array  is %@”,  a array);

// String Appending:

  NSMutableString *mutStr =  [NS Mutable string StringwithString  : @      “Hai”];
    [  mutStr  appendString :@  “how r you”];
  Ns log (@   “  the mut Str after appending is %@;  mut Str);
// String   replacing
// hi how r you
  Int no of Replacing  =   [MutStr replaceOccurrencesOfString:  @  “r” 
                                            With  String  :@  “are”  options: nil range
                                              :  NSMakeRange (0,[MutStr length])];
Ns log (@    “mutStr after replacing  is %@;  mutStr);

Drawing the string in a Rect:

[mutStr   drawInRect :   CG RectMake (0,0,100,40)with font:16]


Get Updates on Tech posts, Interview & Certification questions and training schedules