Monday, July 15, 2013

NSString Methods in Objective-C

Using Strings in Objective C

As we know that String values are given in double quotes in java and other language .
For example "Welcome to IOS Tutorials".

But in objective c, it is different string value, we have to use "@" symbol to represent a string value before the quotes. Example is given below.
NSLog(@"Welcome to IOS Tutorials"); 

 Creating String object

In Objective C, two types of string Objects are there one is mutable and Immutable.
 For create the Immutable string we have to use NSString and to create mutable we have to use NSMutableString. If we are creating a string object using NSString, it is called  immutable string object. It means this string can only assigned only once, that string cannot subsequently be modified in any way.
 NSString *string1 = @"immutable String";


For Mutable string object, we use NSMutableString class. NSMutableString is a subclass of NSString.
It is different to assign a mutable string object.
for example
NSMutableString *string2 = [NSMutableString stringWithString:@" Mutable String "];
or
NSMutableString *string2 = [NSMutableString stringWithString: string1];// string1 is a NSString(immutable ).
Copying a Strings in Objective C

NSMutableString *str1;
NSMutableString *str2;
str1 = [NSMutableString stringWithString: @"This is a string"];
str2 = str1;
NSLog (@"string1 = %@", str1);
NSLog (@"string2 = %@", str2);

Finding a Substring

NSString *str1 = @"IOS tutorial generator";
NSRange range;
range = [str1 rangeOfString: @"tutorial generator"];
NSLog (@"found at index %lu", range.location);
NSLog (@"length = %lu", range.length);


Appending String

NSString *str1 = @"IOS tutorial generator";
NSMutableString *str2;
[str2 appendString: @" Tutorial for beginners"];


Replacing in String

NSMutableString *str1 = [NSMutableString stringWithString: @"IOS tutorial generator"];
[str1 replaceCharactersInRange: NSMakeRange(1, 4) withString: @"Objective C"];
NSLog (@"string1 = %@", string1);


Search and Replacing String

NSMutableString *str1 = [NSMutableString stringWithString: @"Objective C tutorial generator"];
[str1 replaceCharactersInRange: [string1 rangeOfString: @"Objective C"] withString: @"IOS"];


Deleting in String

NSMutableString *str1 = [NSMutableString stringWithString: @"Objective C tutorial generator"];
[str1 deleteCharactersInRange: [str1 rangeOfString: @"Objective C"]];


Return a word Subsection of a String

NSMutableString *st1 = [NSMutableString stringWithString: @"Objective C tutorial generator"];
NSString *st2;
str2 = [str1 substringWithRange: NSMakeRange (4, 5)];
NSLog (@"str2 = %@", string2);

NSMutableString *str1 = [NSMutableString stringWithString: @"Objective C tutorial generator"];
NSString *str2;
str2 = [str1 substringFromIndex: 4];

Inserting a string into a String

NSMutableString *str1 = [NSMutableString stringWithString: @"Objective C tutorial generator"];
[str1 insertString: @", IOS " atIndex: 12];

Comparing Strings

NSString *str1 = @"Hello";
NSString *str2 = @"HHello";
if ([string1 isEqualToString: string2])
        NSLog (@"String are Equal");
else
        NSLog (@"String are not Equal");


Find String Prefixes and Suffixes

NSString *str1 = @"Objective C tutorial generator";
BOOL val;
val = [str1 hasPrefix: @"Objective"];
if (val)
        NSLog (@"Yes it has prefix as Objective");
val = [str1 hasSuffix: @"generator"];
if (val)
        NSLog (@"Yes it has Suffix as generator");

Converting to Upper or Lower Case


NSString *str1 = @"Objective c tutorials for beginners";
NSString *str2;
string2 = [string1 capitalizedString];
Output: Objective C Tutorials For Beginners


lowercaseString

NSString *str1 = @"Objective c tutorials for beginners";
NSString *str2;
str2 = [str1 lowercaseString];
Output: objective c tutorials for beginners


uppercaseString

NSString *str1 = @"Objective c tutorials for beginners";
NSString *str2;
str2 = [str1 uppercaseStringg];
Output: OBJECTIVE C TUTORIALS FOR BEGINNERS


Converting Strings to Numbers

NSString *str1 = @"100";
int IntVal = [str1 intValue];
NSLog (@"%i", IntVal);

Convert String to double

NSString *str1 = @"100.1998";
double Doubleval = [str1 doubleValue];
NSLog (@"%f", Doubleval);

Convert String to float

NSString *str1 = @"10.1092";
float Floatval = [str1 floatValue];
NSLog (@"%f", Floatval);

Convert String to NSInteger

NSString *str1 = @"10";
NSInteger Integerval = [str1 integerValue];
NSLog (@"%li", Integerval);

Converting a String Object to ASCII

NSString *str1 = @"The quick browen fox";
const char *utfStringval = [str1 UTF8String];
printf ("Converted string = %s\n", utfStringval);

No comments:

Post a Comment