Foundation - strings

来源:互联网 发布:卡尔加里 知乎 编辑:程序博客网 时间:2024/05/01 03:05

strings



#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    // insert code here...
    NSLog(@"Hello, World!");
   
    NSString *str1=[[NSString alloc]initWithString:@"a string"];
    NSString *str2=[[NSString alloc]initWithCString:"a Cstring"];
    NSString *str3=[[NSString alloc]initWithFormat:@"4+2=%d", 4+2];

    //convenient allocator
    NSString *str4=[NSString stringWithFormat:@"name: %s, Age: %d","John Doe", 42];
   
    NSLog(@"str1 is: %@", str1);
    NSLog(@"str2 is: %@", str2);
    NSLog(@"str3 is: %@", str3);
    NSLog(@"str4 is: %@", str4);
   
    NSLog(@"str5 length is: %d",[@"i love Cocoa" length]);
   
    [str1 release];
    [str2 release];
    [str3 release];

    //autorelease
    //[str4 release];

   
    [pool release];

    return 0;
}


.....

 // The literal syntax for an NSString object
NSString *str = @"Hello";
// Create one string from another string
NSString *str2 = [NSString stringWithString:str];
// You can also create a string using printf style formatting
str = [NSString stringWithFormat:@"%d potatoes", 10];
// The contents of a text file may be used to initialize a string
str = [NSString stringWithContentsOfFile:@"/path/to/file"];
// C character arrays may be used to create a string as well
char *cStr = "Hello again";
str = [NSString stringWithCString:cStr];
// How to get a C string from an NSString
char cStr = [str UTFString];
// Determine the length of a string, which is a count of the
// number of Unicode characters in the string
unsigned int strLength = [str length];
// Append one NSString to another
// str2 = "Hello, World!"
str2 = [str stringByAppendingString:@", World!"];
// Append a format to an NSString
// str3 = "Hello, World! 2003"
NSString *str3 = [str2 stringByAppendingFormat:@" %d", 2003];
// Extract substrings; returns characters 6 to the end
// subStr = @"World! 2003"
NSString *subStr = [str3 substringFromIndex7];
// Returns characters from beginning to character 5
// subStr = @"Hello"
subStr = [str3 substringToIndex:5];
// Returns 6 characters starting at index 7;
// Also see the comment that accompanies NSRange
// subStr = @"World!"
subStr = [str3 substringWithRange:NSMakeRange(7, 6)];
// Case conversion; returns capitalization: "Hello, World"
NSString *firstcaps = [str2 capitalizedString];
// Case conversion; returns lowercase: "hello, world!"
NSString *lower = [str2 lowercaseString];
// Case conversion; returns uppercase: "HELLO, WORLD!"
NSString *upper = [str2 uppercaseString];
// Searching for substrings; returns NSRange {0, 2}
NSRange loc = [str2 rangeOfString:@"He"];
// Searching for substrings; returns NSRange {NSNotFound, 0}
loc = [str2 rangeOfString:@"and"];
// Checking whether a string is a prefix or suffix of another
BOOL r = [str2 hasPrefix:@"Hello, W"];   // Returns YES
BOOL r = [str2 hasSuffix:@"What?"];      // Returns NO
原创粉丝点击