Creating and Initializing Strings

+ string

Returns an empty string.

Example

NSString *string = [NSString string];
NSLog(@"String:%@",string);
		

Output

2014-04-02 07:35:51.344 iOS-Tutorial[1255:a0b] String:
		

- init

Returns an initialized NSString object that contains no characters.

Example

NSString *string = [[NSString alloc]init];
NSLog(@"String:%@",string);
		

Output

2014-04-02 07:36:46.406 iOS-Tutorial[1269:a0b] String:
		

- initWithBytes: length: encoding:

Returns an initialized NSString object containing a given number of bytes from a given buffer of bytes interpreted in a given encoding.

Example

NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];
const void *bytes = [data bytes];
NSString *string = [[NSString alloc]initWithBytes:bytes length:4 encoding:NSUTF8StringEncoding];
NSLog(@"String:%@",string);		
	

Output

2014-04-02 07:40:11.284 iOS-Tutorial[1283:a0b] String:test
		

- initWithBytesNoCopy: length: encoding: freeWhenDone:

Returns an initialized NSString object that contains a given number of bytes from a given buffer of bytes interpreted in a given encoding, and optionally frees the buffer.

Example

NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *mutableData = [[NSMutableData alloc]init];
[mutableData appendData:data];
void *bytes = [mutableData mutableBytes];
NSString *string = [[NSString alloc]initWithBytesNoCopy:bytes length:4 encoding:NSUTF8StringEncoding freeWhenDone:YES];
NSLog(@"String:%@",string);
		

Output

2014-04-02 07:50:00.367 iOS-Tutorial[1306:a0b] String:test
		

- initWithCharacters: length:

Returns an initialized NSString object that contains a given number of characters from a given C array of Unicode characters.

Example

unichar unichar = 0x91;
NSString *string = [[NSString alloc]initWithCharacters:&unichar length:4];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:06:58.483 iOS-Tutorial[1449:a0b] String:‘䛄
		

- initWithCharactersNoCopy: length: freeWhenDone:

Returns an initialized NSString object that contains a given number of characters from a given C array of Unicode characters.

Example

unichar unichar = 0x91;
NSString *string = [[NSString alloc]initWithCharactersNoCopy:&unichar length:4 freeWhenDone:NO];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:08:40.269 iOS-Tutorial[1478:a0b] String:‘䛄
		

- initWithCString: encoding:

Returns an NSString object initialized using the characters in a given C array, interpreted according to a given encoding.

Example

NSString *string = [[NSString alloc]initWithCString:"test" encoding:NSUTF8StringEncoding];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:11:33.440 iOS-Tutorial[1491:a0b] String:test
		

- initWithUTF8String:

Returns an NSString object initialized by copying the characters from a given C array of UTF8-encoded bytes.

Example

NSString *string = [[NSString alloc]initWithUTF8String:"test©"];
NSLog(@"String:%@",string);		

Output

2014-04-02 08:20:23.717 iOS-Tutorial[1544:a0b] String:test©
		

- initWithFormat:

Returns an NSString object initialized by using a given format string as a template into which the remaining argument values are substituted.

Example

NSString *string = [[NSString alloc]initWithFormat:@"Test:%@ %d",@"eezy",10];
NSLog(@"String:%@",string);		
	

Output

2014-04-02 08:22:58.075 iOS-Tutorial[1557:a0b] String:Test:eezy 10
		

- initWithData: encoding:

Returns an NSString object initialized by converting given data into Unicode characters using a given encoding.

Example

NSData *data = [@"eezy" dataUsingEncoding:NSUTF16BigEndianStringEncoding];
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF16BigEndianStringEncoding];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:36:01.896 iOS-Tutorial[1620:a0b] String:eezy
		

+ stringWithFormat:

Returns a string created by using a given format string as a template into which the remaining argument values are substituted.

Example

NSString *string = [NSString stringWithFormat:@"Test:%@ %d",@"eezy",10];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:37:39.454 iOS-Tutorial[1634:a0b] String:Test:eezy 10
		

+ localizedStringWithFormat:

Returns a string created by using a given format string as a template into which the remaining argument values are substituted according to the user's default locale.

Example

NSString *string = [NSString localizedStringWithFormat:@"Test:%@ %d",@"eezy",10];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:38:30.815 iOS-Tutorial[1647:a0b] String:Test:eezy 10
		

+ stringWithCharacters: length:

Returns a string containing a given number of characters taken from a given C array of Unicode characters.

Example

unichar unichar = 0x91;
NSString *string = [NSString stringWithCharacters:&unichar length:4];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:40:03.601 iOS-Tutorial[1660:a0b] String:‘䛄
		

+ stringWithCString: encoding:

Returns a string containing the bytes in a given C array, interpreted according to a given encoding.

Example

NSString *string = [NSString stringWithCString:"eezy" encoding:NSUTF8StringEncoding];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:42:26.007 iOS-Tutorial[1675:a0b] String:eezy
		

+ stringWithUTF8String:

Returns a string created by copying the data from a given C array of UTF8-encoded bytes.

Example

NSString *string = [NSString stringWithUTF8String:"test©"];
NSLog(@"String:%@",string);
		

Output

2014-04-02 08:44:09.042 iOS-Tutorial[1690:a0b] String:test©
		

Creating and Initializing a String from a File

+ stringWithContentsOfFile: encoding: error:

Returns a string created by reading data from the file at a given path interpreted using a given encoding. Let us assume we have a text file named sampleString.txt as follows.

test©
		

Example

 NSString *file = [[NSBundle mainBundle] pathForResource:@"sampleString" ofType:@"txt"];
    NSString *string = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"String:%@",string);
		

Output

2014-04-02 09:09:09.584 iOS-Tutorial[1765:a0b] String:test©
		

- initWithContentsOfFile: encoding: error:

Returns an NSString object initialized by reading data from the file at a given path using a given encoding.

Example

NSString *string = [[NSString alloc] initWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
NSLog(@"String:%@",string);
		

Output

2014-04-02 09:12:22.902 iOS-Tutorial[1857:a0b] String:test©
		

Creating and Initializing a String from an URL

+ stringWithContentsOfURL: encoding: error:

Returns a string created by reading data from a given URL interpreted using a given encoding.

Example

NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-string"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"String:%@",string);
		

Output

			// Implementation in progress
		

- initWithContentsOfURL: encoding: error:

Returns an NSString object initialized by reading data from a given URL interpreted using a given encoding.

Example

NSString *string = [[NSString alloc ]initWithContentsOfURL:[NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-string"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"String:%@",string);
		

Output

2014-04-02 09:31:06.479 iOS-Tutorial[2257:a0b] String:test
		

Writing to a File

- writeToFile: atomically: encoding: error:

Writes the contents of the receiver to a file at a given path using a given encoding.

Example

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *filePath = [documentsURL.path stringByAppendingPathComponent:@"sample-string.txt"];
NSString *string = @"test";
NSLog(@"Write Status:%d",[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]);		
	

Output

2014-04-02 20:40:18.426 iOS-Tutorial[481:a0b] Write Status:1
		
>

Getting a String’s Length

- length

Returns the number of Unicode characters in the receiver.

Example

NSString *string = @"test";
NSLog(@"Length:%d",string.length);
		

Output

2014-04-02 21:06:21.871 iOS-Tutorial[523:a0b] Length:4
		

- lengthOfBytesUsingEncoding:

Returns the number of bytes required to store the receiver in a given encoding.

Example

NSString *string = @"\u2022test";
NSLog(@"Length:%d %@",[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding],string);
		

Output

2014-04-02 21:10:33.300 iOS-Tutorial[569:a0b] Length:7 •test
		

- maximumLengthOfBytesUsingEncoding:

Returns the maximum number of bytes needed to store the receiver in a given encoding.

Example

 NSString *string = @"\u2022test";
 NSLog(@"Length:%d %@",[string maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding],string);
		

Output

2014-04-02 21:11:21.736 iOS-Tutorial[580:a0b] Length:15 •test
		

Getting Characters and Bytes

- characterAtIndex:

Returns the character at a given array position.

Example

    NSString *string = @"test";
    NSLog(@"%c",[string characterAtIndex:0]);
		

Output

2014-04-02 21:14:32.052 iOS-Tutorial[597:a0b] t
		

Getting C Strings

- cStringUsingEncoding:

Returns a representation of the receiver as a C string using a given encoding.

Example

    NSString *string = @"eezy";
    NSLog(@"%s",[string cStringUsingEncoding:[string fastestEncoding]])
		

Output

2014-04-02 21:24:00.813 iOS-Tutorial[622:a0b] eezy
		

- UTF8String

Returns a null-terminated UTF8 representation of the receiver.

Example

    NSString *string = @"eezy\u2000";
    NSLog(@"%s",[string UTF8String]);
		

Output

2014-04-02 21:29:58.283 iOS-Tutorial[708:a0b] eezy‚ÄÄ
		

Combining Strings

- stringByAppendingFormat:

Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.

Example

NSString *string = @"Eezy";
NSLog(@"%@",[string stringByAppendingFormat:@" Tutorials %@",@"Website"]);		
	

Output

2014-04-02 21:35:32.310 iOS-Tutorial[742:a0b] Eezy Tutorials Website
		

- stringByAppendingString:

Returns a new string made by appending a given string to the receiver.

Example

NSString *string = @"eezy";
NSLog(@"%@",[string stringByAppendingString:@" Tutorials"]);
		

Output

2014-04-02 21:31:27.573 iOS-Tutorial[727:a0b] eezy Tutorials
		

- stringByPaddingToLength: withString: startingAtIndex:

Returns a new string formed from the receiver by either removing characters from the end, or by appending as many occurrences as necessary of a given pad string.

Example

NSString *string = @"ios-Eezy";
NSLog(@"%@",[string stringByPaddingToLength:25 withString:@"Tutorials Website " startingAtIndex:4]);
		

Output

2014-04-02 21:42:59.012 iOS-Tutorial[828:a0b] EezyTutorials Website
		

Dividing Strings

- componentsSeparatedByString:

Returns an array containing substrings from the receiver that have been divided by a given separator.

Example

 NSString *string = @"ios-Eezy-Tutorials";
 NSLog(@"%@",[string componentsSeparatedByString:@"-"]);
		

Output

2014-04-03 22:25:17.176 iOS-Tutorial[932:a0b] (
    ios,
    Eezy,
    Tutorials
)		

- componentsSeparatedByCharactersInSet:

Returns an array containing substrings from the receiver that have been divided by characters in a given set.

Example

NSString *string = @"ios-Eezy,Tutorials";
NSLog(@"%@",[string componentsSeparatedByCharactersInSet:[NSCharacterSet punctuationCharacterSet]]);
		

Output

2014-04-03 22:27:05.546 iOS-Tutorial[974:a0b] (
    ios,
    Eezy,
    Tutorials
)		

- stringByTrimmingCharactersInSet:

Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

Example

NSString *string = @",ios-Eezy,Tutorials,";
NSLog(@"%@",[string stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]]);
		

Output

2014-04-03 22:28:07.750 iOS-Tutorial[996:a0b] ios-Eezy,Tutorials
		

- substringFromIndex:

Returns a new string containing the characters of the receiver from the one at a given index to the end.

Example

NSString *string = @"ios-EezyTutorials";
NSLog(@"%@",[string substringFromIndex:4]);
		

Output

2014-04-03 22:28:49.210 iOS-Tutorial[1013:a0b] EezyTutorials
		

- substringWithRange:

Returns a string object containing the characters of the receiver that lie within a given range.

Example

NSString *string = @"ios-EezyTutorials";
NSLog(@"%@",[string substringWithRange:NSMakeRange(4, 12)]);
		

Output

2014-04-03 22:29:50.420 iOS-Tutorial[1036:a0b] EezyTutorial
		

- substringToIndex:

Returns a new string containing the characters of the receiver up to, but not including, the one at a given index.

Example

    NSString *string = @"ios-EezyTutorials";
    NSLog(@"%@",[string substringToIndex:3]);
		

Output

2014-04-03 22:30:33.219 iOS-Tutorial[1047:a0b] ios
		

Finding Characters and Substrings

- rangeOfCharacterFromSet:

Finds and returns the range in the receiver of the first character from a given character set.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet punctuationCharacterSet]];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:33:17.472 iOS-Tutorial[1071:a0b] {3, 1}
		

- rangeOfCharacterFromSet:options:

Finds and returns the range in the receiver of the first character, using given options, from a given character set.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"IOSEZY"] options:NSCaseInsensitiveSearch];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:35:09.708 iOS-Tutorial[1084:a0b] {4, 1}
		

- rangeOfCharacterFromSet: options: range:

Finds and returns the range in the receiver of the first character from a given character set found in a given range with given options.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"IOSEZY"] options:NSCaseInsensitiveSearch range:NSMakeRange(4, 10)];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:36:53.501 iOS-Tutorial[1125:a0b] {4, 1}
		

- rangeOfString:

Finds and returns the range of the first occurrence of a given string within the receiver.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfString:@"Eezy"];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:37:55.326 iOS-Tutorial[1139:a0b] {4, 4}
		

- rangeOfString: options:

Finds and returns the range of the first occurrence of a given string within the receiver, subject to given options.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfString:@"ee" options:NSCaseInsensitiveSearch];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:39:21.199 iOS-Tutorial[1167:a0b] {4, 2}
		

- rangeOfString: options: range:

Finds and returns the range of the first occurrence of a given string, within the given range of the receiver, subject to given options.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfString:@"ee" options:NSCaseInsensitiveSearch range: NSMakeRange(3, 10) ];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:41:19.034 iOS-Tutorial[1183:a0b] {4, 2}
		

- rangeOfString: options: range: locale:

Finds and returns the range of the first occurrence of a given string within a given range of the receiver, subject to given options, using the specified locale, if any.

Example

NSString *string = @"ios-EezyTutorials";
NSRange range = [string rangeOfString:@"ee" options:NSCaseInsensitiveSearch range: NSMakeRange(3, 10) locale:[NSLocale systemLocale]];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:42:22.281 iOS-Tutorial[1198:a0b] {4, 2}
		

- enumerateLinesUsingBlock:

Enumerates all the lines in a string.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
[string enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
	NSLog(@"%@",line);
}];
		

Output

2014-04-03 22:44:04.000 iOS-Tutorial[1212:a0b] ios
2014-04-03 22:44:04.002 iOS-Tutorial[1212:a0b] EezyTutorials
2014-04-03 22:44:04.002 iOS-Tutorial[1212:a0b] Website
		

- enumerateSubstringsInRange: options: usingBlock:

Enumerates the substrings of the specified type in the specified range of the string.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByLines usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        NSLog(@"%@",substring);
}];
		

Output

2014-04-03 22:45:56.590 iOS-Tutorial[1232:a0b] ios
2014-04-03 22:45:56.592 iOS-Tutorial[1232:a0b] EezyTutorials
2014-04-03 22:45:56.592 iOS-Tutorial[1232:a0b] Website
		

Replacing Substrings

- stringByReplacingOccurrencesOfString: withString:

Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
string = [string stringByReplacingOccurrencesOfString:@"e" withString:@"è"];
NSLog(@"%@",string);
		

Output

2014-04-03 22:47:51.564 iOS-Tutorial[1245:a0b] ios
EèzyTutorials
Wèbsitè
		

- stringByReplacingOccurrencesOfString: withString: options: range:

Returns a new string in which all occurrences of a target string in a specified range of the receiver are replaced by another given string.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
string = [string stringByReplacingOccurrencesOfString:@"e" withString:@"è" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
NSLog(@"%@",string);
		

Output

2014-04-03 22:49:23.869 iOS-Tutorial[1270:a0b] ios
èèzyTutorials
Wèbsitè
		

- stringByReplacingCharactersInRange: withString:

Returns a new string in which the characters in a specified range of the receiver are replaced by a given string.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
string = [string stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@"wow !"];
NSLog(@"%@",string);
		

Output

2014-04-03 22:50:48.051 iOS-Tutorial[1286:a0b] wow !
EezyTutorials
Website
		

Determining Line and Paragraph Ranges

- lineRangeForRange:

Returns the range of characters representing the line or lines containing a given range.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
NSRange range = [string lineRangeForRange:NSMakeRange(4, 10)];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:55:23.161 iOS-Tutorial[1312:a0b] {4, 14}
		

- paragraphRangeForRange:

Returns the range of characters representing the paragraph or paragraphs containing a given range.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
NSRange range = [string paragraphRangeForRange:NSMakeRange(4, string.length-4)];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:57:06.360 iOS-Tutorial[1359:a0b] {4, 21}
		

Determining Composed Character Sequences

- rangeOfComposedCharacterSequenceAtIndex:

Returns the range in the receiver of the composed character sequence located at a given index.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
NSRange range = [string rangeOfComposedCharacterSequenceAtIndex:0];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:58:21.888 iOS-Tutorial[1390:a0b] {0, 1}
		

- rangeOfComposedCharacterSequencesForRange:

Returns the range in the string of the composed character sequences for a given range.

Example

NSString *string = @"ios\nEezyTutorials\nWebsite";
NSRange range = [string rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 18)];
NSLog(@"%@",NSStringFromRange(range));
		

Output

2014-04-03 22:59:30.312 iOS-Tutorial[1402:a0b] {0, 18}
		

Converting String Contents Into a Property List

- propertyList

Parses the receiver as a text representation of a property list, returning an NSString, NSData, NSArray, or NSDictionary object, according to the topmost element.

Example

NSString *string = @"\n\nEezy\nTutorials\n\n";
NSLog(@"%@",[string propertyList]);
		

Output

2014-04-03 23:02:38.150 iOS-Tutorial[1446:a0b] (
    Eezy,
    Tutorials
)
		

- propertyListFromStringsFileFormat

Returns a dictionary object initialized with the keys and values found in the receiver.

Example

NSString *string = @"key1Eezykey2Tutorials";
NSLog(@"%@",[string propertyListFromStringsFileFormat]);
		

Output

2014-04-03 23:06:46.978 iOS-Tutorial[1500:a0b] {
    key1 = Eezy;
    key2 = Tutorials;
}
		

Identifying and Comparing Strings

- caseInsensitiveCompare:

Returns the result of invoking compare:options: (page 1700) with NSCaseInsensitiveSearch as the only option.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string caseInsensitiveCompare:@"ios eezy tutorials"];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString);
		

Output

2014-04-03 23:10:59.029 iOS-Tutorial[1540:a0b] Same
		

- localizedCaseInsensitiveCompare:

Compares the string and a given string using a case-insensitive, localized, comparison.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string localizedCaseInsensitiveCompare:@"ios èezy tutorials"];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString)
		

Output

2014-04-03 23:12:07.341 iOS-Tutorial[1570:a0b] Diff
		

- compare:

Returns the result of invoking compare:options:range: (page 1701) with no options and the receiver’s full extent as the range.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string compare:@"ios eezy tutorials"];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString);
		

Output

2014-04-03 23:13:40.707 iOS-Tutorial[1595:a0b] Diff
		

- localizedCompare:

Compares the string and a given string using a localized comparison.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string localizedCompare:@"ios eezy tutorials"];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString);
		

Output

2014-04-03 23:13:40.707 iOS-Tutorial[1595:a0b] Diff
		

- compare:options:

Compares the string with the specified string using the given options.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string compare:@"ios eezy tutorials" options:NSDiacriticInsensitiveSearch];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString);
		

Output

2014-04-03 23:14:59.907 iOS-Tutorial[1610:a0b] Diff
		

- compare:options:range:

Returns the result of invoking compare:options:range:locale: with a nil locale.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string compare:@"ios eezy tutorials" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 2)];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString);
		

Output

2014-04-03 23:17:13.397 iOS-Tutorial[1654:a0b] Diff
		

- compare: options: range: locale:

Compares the string using the specified options and returns the lexical ordering for the range.

Example

NSString *string = @"iOS Eezy Tutorials";
NSComparisonResult result =[string compare:@"ios eezy tutorials" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 2) locale:[NSLocale systemLocale]];
NSString *resultString = result==0?@"Same":@"Diff";
NSLog(@"%@",resultString);
		

Output

2014-04-03 23:18:54.755 iOS-Tutorial[1669:a0b] Diff
		

- hasPrefix:

Returns a Boolean value that indicates whether a given string matches the beginning characters of the receiver.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%d",[string hasPrefix:@"i"]);
		

Output

2014-04-03 23:20:16.509 iOS-Tutorial[1700:a0b] 1
		

- hasSuffix:

Returns a Boolean value that indicates whether a given string matches the ending characters of the receiver.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%d",[string hasSuffix:@"ls"]);
		

Output

2014-04-03 23:20:50.873 iOS-Tutorial[1711:a0b] 1
		

- isEqualToString:

Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%d",[string isEqualToString:@"iOS Eezy Tutorials"]);
		

Output

2014-04-03 23:21:28.269 iOS-Tutorial[1724:a0b] 1
		

- hash

Returns an unsigned integer that can be used as a hash table address.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%d",[string hash]);
		

Output

2014-04-03 23:22:11.557 iOS-Tutorial[1743:a0b] 477433025
		

Folding Strings

- stringByFoldingWithOptions:locale:

Returns a string with the given character folding options applied.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string stringByFoldingWithOptions:NSCaseInsensitiveSearch locale:[NSLocale systemLocale]]);
		

Output

2014-04-03 23:23:16.124 iOS-Tutorial[1754:a0b] ios eezy tutorials
		

Getting a Shared Prefix

- commonPrefixWithString:options:

Returns a string containing characters the receiver and a given string have in common, starting from the beginning of each up to the first characters that aren’t equivalent.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string commonPrefixWithString:@"iOS" options:NSCaseInsensitiveSearch]);
		

Output

2014-04-03 23:24:21.709 iOS-Tutorial[1773:a0b] iOS
		

Changing Case

- capitalizedString

Returns a capitalized representation of the receiver.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string capitalizedString]);
		

Output

2014-04-03 23:25:04.975 iOS-Tutorial[1786:a0b] Ios Eezy Tutorials
		

- capitalizedStringWithLocale:

Returns a capitalized representation of the receiver using the specified locale.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string capitalizedStringWithLocale:[NSLocale systemLocale]]);
		

Output

2014-04-03 23:25:44.217 iOS-Tutorial[1798:a0b] Ios Eezy Tutorials
		

- lowercaseString

Returns lowercased representation of the receiver.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string lowercaseString]);
		

Output

2014-04-03 23:26:16.938 iOS-Tutorial[1810:a0b] ios eezy tutorials
		

- lowercaseStringWithLocale:

Returns a version of the string with all letters converted to lowercase, taking into account the specified locale.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string lowercaseStringWithLocale:[NSLocale systemLocale]]);
		

Output

2014-04-03 23:26:53.881 iOS-Tutorial[1822:a0b] ios eezy tutorials
		

- uppercaseString

Returns a uppercased representation of the receiver.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string uppercaseString]);
		

Output

2014-04-03 23:27:24.103 iOS-Tutorial[1834:a0b] IOS EEZY TUTORIALS
		

- uppercaseStringWithLocale:

Returns a version of the string with all letters converted to uppercase, taking into account the specified locale.

Example

NSString *string = @"iOS Eezy Tutorials";
NSLog(@"%@",[string uppercaseStringWithLocale:[NSLocale systemLocale]]);
		

Output

2014-04-03 23:27:55.412 iOS-Tutorial[1851:a0b] IOS EEZY TUTORIALS
		

Getting Numeric Values

- doubleValue

Returns the floating-point value of the receiver’s text as a double.

Example

NSString *string = @"10";
NSLog(@"%f",[string doubleValue]);
		

Output

2014-04-03 23:29:59.290 iOS-Tutorial[1880:a0b] 10.000000
		

- floatValue

Returns the floating-point value of the receiver’s text as a float.

Example

NSString *string = @"10";
NSLog(@"%f",[string floatValue]);
		

Output

2014-04-03 23:30:25.051 iOS-Tutorial[1894:a0b] 10.000000
		

- intValue

Returns the integer value of the receiver’s text.

Example

NSString *string = @"10";
NSLog(@"%d",[string intValue]);
		

Output

2014-04-03 23:30:49.801 iOS-Tutorial[1906:a0b] 10
		

- integerValue

Returns the NSInteger value of the receiver’s text.

Example

NSString *string = @"10";
NSLog(@"%d",[string integerValue]);
		

Output

2014-04-03 23:30:49.801 iOS-Tutorial[1906:a0b] 10
		

- longLongValue

Returns the long long value of the receiver’s text.

Example

NSString *string = @"10";
NSLog(@"%lld",[string longLongValue]);
		

Output

2014-04-03 23:31:59.320 iOS-Tutorial[1937:a0b] 10
		

- boolValue

Returns the Boolean value of the receiver’s text.

Example

NSString *string = @"YES";
NSLog(@"%d",[string boolValue]);
		

Output

2014-04-03 23:32:31.727 iOS-Tutorial[1950:a0b] 1
		

Working with Paths

+ pathWithComponents:

Returns a string built from the strings in a given array by concatenating them with a path separator between each pair.

Example

NSLog(@"%@",[NSString pathWithComponents:@[@"ios.eezytutorials.com",@"sample-files",@"sample-array-plist.plist"]]);
		

Output

2014-04-03 23:36:27.824 iOS-Tutorial[1985:a0b] ios.eezytutorials.com/sample-files/sample-array-plist.plist
		

- pathComponents

Returns an array of NSString objects containing, in order, each path component of the receiver.

Example

NSString *string = @"ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%@",[string pathComponents]);
		

Output

2014-04-03 23:37:35.807 iOS-Tutorial[1997:a0b] (
    "ios.eezytutorials.com",
    "sample-files",
    "sample-array-plist.plist"
)		

- fileSystemRepresentation

Returns a file system-specific representation of the receiver.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%s",[string fileSystemRepresentation]);
		

Output

2014-04-03 23:39:08.694 iOS-Tutorial[2013:a0b] http://ios.eezytutorials.com/sample-files/sample-array-plist.plist
		

- isAbsolutePath

Returning a Boolean value that indicates whether the receiver represents an absolute path.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%d",[string isAbsolutePath]);
		

Output

2014-04-03 23:40:17.089 iOS-Tutorial[2032:a0b] 0
		

- lastPathComponent

Returns the last path component of the receiver.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%@",[string lastPathComponent]);
		

Output

2014-04-03 23:40:41.111 iOS-Tutorial[2045:a0b] sample-array-plist.plist
		

- pathExtension

Interprets the receiver as a path and returns the receiver’s extension, if any.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%@",[string pathExtension]);
		

Output

2014-04-03 23:41:08.636 iOS-Tutorial[2057:a0b] plist
		

- stringByAbbreviatingWithTildeInPath

Returns a new string that replaces the current home directory portion of the current path with a tilde (~) character.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%@",[string stringByAbbreviatingWithTildeInPath]);
		

Output

2014-04-03 23:42:02.748 iOS-Tutorial[2093:a0b] http://ios.eezytutorials.com/sample-files/sample-array-plist.plist
		

- stringByAppendingPathComponent:

Returns a new string made by appending to the receiver a given string.

Example

NSString *string = @"www.eezytutorials.com/sample-files";
NSLog(@"%@",[string stringByAppendingPathComponent:@"sample-array-plist.plist"]);
		

Output

2014-04-03 23:42:45.978 iOS-Tutorial[2103:a0b] http://ios.eezytutorials.com/sample-files/sample-array-plist.plist
		

- stringByAppendingPathExtension:

Returns a new string made by appending to the receiver an extension separator followed by a given extension.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist";
NSLog(@"%@",[string stringByAppendingPathExtension:@"plist"]);
		

Output

2014-04-03 23:43:20.463 iOS-Tutorial[2120:a0b] http://ios.eezytutorials.com/sample-files/sample-array-plist.plist
		

- stringByDeletingLastPathComponent

Returns a new string made by deleting the last path component from the receiver, along with any final path separator.

Example

NSString *string = @"ios.eezytutorials.com/sample-files/sample-array-plist";
NSLog(@"%@",[string stringByDeletingLastPathComponent]);
		

Output

2014-04-03 23:44:01.178 iOS-Tutorial[2132:a0b] ios.eezytutorials.com/sample-files		

- stringByDeletingPathExtension

Returns a new string made by deleting the extension (if any, and only the last) from the receiver.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%@",[string stringByDeletingPathExtension]);
		

Output

2014-04-03 23:44:33.119 iOS-Tutorial[2144:a0b] www.eezytutorials.com/sample-files/sample-array-plist
		

- stringByExpandingTildeInPath

Returns a new string made by expanding the initial component of the receiver to its full path value.

Example

NSString *string = @"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist";
NSLog(@"%@",[string stringByExpandingTildeInPath]);
		

Output

2014-04-03 23:45:09.668 iOS-Tutorial[2157:a0b] http://ios.eezytutorials.com/sample-files/sample-array-plist.plist
		

Advertisements