-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog15.4.m
42 lines (32 loc) · 1.26 KB
/
prog15.4.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Basic String Operations - Continued
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *str1 = @"This is a string A";
NSString *str2 = @"This is a string B";
NSString *res;
NSRange subRange;
// Extract first 3 chars from string
res = [str1 substringToIndex: 3];
NSLog(@"First 3 chars of str1: %@", res);
// Extract chars to end of string starting at index 5
res = [str1 substringFromIndex: 5];
NSLog(@"Chars from index 5 of str1: %@", res);
// Extract chars from index 8 through 13 (6 chars)
res = [[str1 substringFromIndex: 8] substringToIndex: 6];
NSLog(@"Chars from index 8 through 13: %@", res);
// An easier way to to the same thing
res = [str1 substringWithRange: NSMakeRange(8,6)];
NSLog(@"Chars from index 8 through 13: %@", res);
// Locate one string inside another
subRange = [str1 rangeOfString: @"string A"];
NSLog(@"String is at index %lu, length is %lu", subRange.location, subRange.length);
subRange = [str1 rangeOfString: @"string B"];
if (subRange.location == NSNotFound)
NSLog(@"String not found");
else
NSLog(@"String is at index %lu, length is %lu", subRange.location, subRange.length);
[pool drain];
return 0;
}