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.
//initialization:
NSString * str = @ “hello”; Str = [NSString StringwithString : @ “hello”]; NSLog (@ “the string is @”, str);
//formatting:
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”);
Subscribe to our youtube channel to get new updates..!
//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”); } Else Ns log (@ “the string are not equal”);
o/p:
// 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]
Frequently Asked iPhone Interview Questions & Answers