Objective-C basic notes (8) common Foundation classes NSString

Source: Internet
Author: User

Objective-C basic notes (8) common Foundation classes NSString

I. method for creating strings

Void stringCreate () {// method 1 NSString * str1 = @ "A String! "; // Method 2 NSString * str2 = [[[NSString alloc] init] autorelease]; str2 = @" A String! "; [Str2 release]; // method 3 NSString * str3 = [[NSString alloc] initWithString: @" A String! "]; [Str3 release]; // after the memory is released, str3 points to the new memory str3 = [NSString stringWithString: @" A String! "]; // Method 4 NSString * str4 = [[NSString alloc] initwithuf8string:" A String! "]; [Str4 release]; // Method 5 NSString * str5 = [[NSString alloc] initWithFormat: @" My age is % I and height is %. 2f ", 19, 1.55f]; [str5 release]; // str5 points to the new memory str5 = [NSString stringWithFormat: @" My age is % I and height is %. 2 ", 19, 1.55f];}
Note that

1. When directing to the new memory, ensure that the memory originally pointed to is released.
2. After a String object is created using the static method, the system will automatically release the memory. Do not release it again. Otherwise, a wild pointer error will occur.

Ii. Read the file content to a string

// Read the string from the file and use the UTF-8 encoding NSError * error; NSString * path = @ "/user/apple/Desktop/test.txt"; NSString * str1 = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: & error]; if (error) {NSLog (@ "File Read failed");} else {NSLog (@ "File Read succeeded ");}
Note that the address of the error must be entered when an error is passed in.


As shown in, if we have defined the structure of a (int a = 123) variable in the memory, we need to change the value of variable, find the address and assign a value to the memory pointed to by the address. Based on this, we need to pass the error address to get the error information.

3. Read the URL content to a string

    NSString *str2 = [NSString stringWithContentsOfURL:@"file:///user/apple/Desktop/test.txt" encoding:NSUTF8StringEncoding error:&error];        NSString *str3 = [NSString stringWithContentsOfURL:@"http://www.baidu.com" encoding:NSUTF8StringEncoding error:&error];
Iv. Export strings

# Export void stringExport () {NSError * error; NSString * str = @ "dfdfddfdfdfd"; NSString * path = @ "/user/apple/Desktop/test.txt "; // YES indicates that an intermediate temporary file is created for atomic operations. // if the file does not exist, [str writeToFile: path atomically: YES encoding: NSUTF8StringEncoding error: & error]; if (error) {NSLog (@ "Write failed: % @", [error localizedDescription]);} else {NSLog (@ "imported successfully! ");}}
Note that

1. If the written file does not exist, it will be automatically created

2. An error occurs if the folder does not exist or the path is incorrect.

3. atomically indicates atomicity. If it is set to YES, all strings are first written to the temporary file, and finally all are written to the target file, which can prevent file write interruption errors.

5. Variable string

NSString is similar to String in Java. The following describes a variable length String of NSMutableString.

# Create void mutableStringCreate () For The pragma mark variable string {// a storage space with a pre-allocated 10 characters NSMutableString * str = [[NSMutableString alloc] initWithCapacity: 10]; // set the string content [str setString: @ "1234"]; // concatenate a string [str appendString: @ "567"]; [str appendFormat: @ "age is % I", 27]; // replace string nsange range = [str rangeOfString: @ "age"]; // obtain the [str replaceCharactersInRange: range withString: @ "number"]; // Insert the string [str insertString: @ "abc" atIndex: 2]; // 12abc34 .... // Delete the string [str deleteCharactersInRange: [range]; [str release];}
NSMutableString is a subclass of NSString. Therefore, NSMutableString, a non-private method of NSString, can be used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.