Basic NSString and NSMutableString usage, nsmutablestring

Source: Internet
Author: User

Basic NSString and NSMutableString usage, nsmutablestring

Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send

Game official download: http://dwz.cn/RwTjl

Game video preview: http://dwz.cn/RzHHd

Game Development blog: http://dwz.cn/RzJzI

Game source code transfer: http://dwz.cn/Nret1


NSString is actually an object type. NSString is a subclass of NSObject (Basic Object of Cocoa Foundation)

1. Create NSString

1. Create a constant string.
NSString * astring = @ "This is a String! ";

2. Create an empty string and assign a value.
NSString * astring = [[NSString alloc] init];
Astring = @ "This is a String! ";
[Astring release];
NSLog (@ "astring: % @", astring );

NSString * astring = [[NSString alloc] init];
NSLog (@ "0x %. 8x", astring );
Astring = @ "This is a String! ";
NSLog (@ "0x %. 8x", astring );
[Astring release];
NSLog (@ "astring: % @", astring );

3. In the above method, the speed is improved: initWithString Method
NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "];
NSLog (@ "astring: % @", astring );
[Astring release];

4. Use Standard c to create a string: initWithCString Method
Char * Cstring = "This is a String! ";
NSString * astring = [[NSString alloc] initWithCString: Cstring];
NSLog (@ "astring: % @", astring );
[Astring release];

5. Create a formatted string: A placeholder (consisting of one % and one character)
Int I = 1;
Int j = 2;
NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ "% d. This is % I string! ", I, j];
NSLog (@ "astring: % @", astring );
[Astring release];

6. Create a temporary string
NSString * astring;
Astring = [NSString stringWithCString: "This is a temporary string"];
NSLog (@ "astring: % @", astring );

7. Write a string to a file: writeToFile Method
NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "];
NSLog (@ "astring: % @", astring );
NSString * path = @ "astring. text ";
[Astring writeToFile: path atomically: YES];
[Astring release];

8. Read the string from the file: initWithContentsOfFile Method
NSString * path = @ "astring. text ";
NSString * astring = [[NSString alloc] initWithContentsOfFile: path];
NSLog (@ "astring: % @", astring );
[Astring release];


2. String comparison

1. Comparison with C: strcmp Function
Char string1 [] = "string! ";
Char string2 [] = "string! ";
If (strcmp (string1, string2) = 0)
{
NSLog (@ "1 ");
}

2. isdomaintostring Method
NSString * astring01 = @ "This is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 isw.tostring: astring02];
NSLog (@ "result: % d", result );

3. compare (three values returned by comparer)
NSString * astring01 = @ "This is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 compare: astring02] = NSOrderedSame;
NSLog (@ "result: % d", result );
// Determine whether the content of NSOrderedSame is the same

NSString * astring01 = @ "This is a String! ";
NSString * astring02 = @ "this is a String! ";
BOOL result = [astring01 compare: astring02] = NSOrderedAscending;
NSLog (@ "result: % d", result );
// NSOrderedAscending determines the size of the two objects (which is compared alphabetically. astring02 is true if it is greater than astring01)

Compare strings with uppercase or lowercase letters
NSString * astring01 = @ "this is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame;
NSLog (@ "result: % d", result );
// NSOrderedDescending determines the size of the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01)

Case-insensitive string 2
NSString * astring01 = @ "this is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 compare: astring02
Options: NSCaseInsensitiveSearch | NSNumericSearch] = NSOrderedSame;
NSLog (@ "result: % d", result );
P.S: NSCaseInsensitiveSearch: case-insensitive comparison NSLiteralSearch: perform a full comparison. case-sensitive NSNumericSearch: Compare the number of characters in a string, not the character value.


3. Rewrite strings

NSString * string1 = @ "A String ";
NSString * string2 = @ "String ";
NSLog (@ "string1: % @", [string1 uppercaseString]); // uppercase
NSLog (@ "string2: % @", [string2 lowercaseString]); // lower case
NSLog (@ "string2: % @", [string2 capitalizedString]); // initial size


4. Search for strings

NSString * string1 = @ "This is a string ";
NSString * string2 = @ "string ";
Nsange range = [string1 rangeOfString: string2];
Int location = range. location;
Int leight = range. length;
NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ "Location: % I, Leight: % I", location, leight];
NSLog (@ "astring: % @", astring );
[Astring release];


5. String Truncation

1.-substringToIndex: captures the string from the beginning to the specified position, but does not include the characters at the position.
NSString * string1 = @ "This is a string ";
NSString * string2 = [string1 substringToIndex: 3];
NSLog (@ "string2: % @", string2 );

2.-substringFromIndex: starts with a specified position (including the characters at the specified position) and includes all subsequent characters.
NSString * string1 = @ "This is a string ";
NSString * string2 = [string1 substringFromIndex: 3];
NSLog (@ "string2: % @", string2 );

3.-substringWithRange: // truncate the substring from the string according to the given position and length.
NSString * string1 = @ "This is a string ";
NSString * string2 = [string1 substringWithRange: NSMakeRange (0, 4)];
NSLog (@ "string2: % @", string2 );

4. Extract the last symbol of NSString.

Method 1.

NSString * str = @ "/Users/yangiori/Library/Application Support/iPhone Simulator/5.1/Applicati *****/8724956B-407E-4ACD-BBA6-95C7D033C33D/Documents/content/chapters/8 ";
NSString * temp1 = [[str componentsSeparatedByString: @ "/"] lastObject];
NSLog (@ "% @", temp1 );

Result: 8

Method 2.

NSString * str = @ "/Users/yangiori/Library/Application Support/iPhone Simulator/5.1/Applicati *****/8724956B-407E-4ACD-BBA6-95C7D033C33D/Documents/content/chapters/8 ";

NSString * temp2 = [str substringFromIndex: [str length]-1];

NSLog (@ "% @", temp2 );

Result: 8

5. Extract A string from a specified position

NSString * str = [NSString stringWithFormat: @ "********************* Documents/imagepolici.jpg", 2];

Nsange range = [str rangeOfString: @ "Documents"];

NSString * result = [str substringFromIndex: range. location];

NSLog (@ "% @", result );


Vi. Other operations

1. extension path
NSString * Path = @"~ /NSData.txt ";
NSString * absolutePath = [Path stringByExpandingTildeInPath];
NSLog (@ "absolutePath: % @", absolutePath );
NSLog (@ "Path: % @", [absolutePath stringByAbbreviatingWithTildeInPath]);

2. File Extension
NSString * Path = @"~ /NSData.txt ";
NSLog (@ "Extension: % @", [Path pathExtension]);


NSMutableString

Basic usage

1. allocate capacity to strings

StringWithCapacity:
NSMutableString * String;
String = [NSMutableString stringWithCapacity: 40];

2. Add characters after an existing string

AppendString: and appendFormat:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];
[String1 appendString: @ ", I will be adding some character"];
[String1 appendFormat: [NSString stringWithFormat: @ ", I will be adding some character"];
NSLog (@ "String1: % @", String1 );

3. Delete characters in an existing string according to the given range and length

DeleteCharactersInRange:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];
[String1 deleteCharactersInRange: NSMakeRange (0, 5)];
NSLog (@ "String1: % @", String1 );

4. Insert the given string at the specified position after the existing string

-InsertString: atIndex:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];
[String1 insertString: @ "Hi! "AtIndex: 0];
NSLog (@ "String1: % @", String1 );

5. Replace existing null strings with other strings.

-SetString:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];
[String1 setString: @ "Hello Word! "];
NSLog (@ "String1: % @", String1 );

6. Replace the original character with the given range and string

-SetString:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];
[String1 replaceCharactersInRange: NSMakeRange (0, 4) withString: @ "That"];
NSLog (@ "String1: % @", String1 );

7. Determine whether the string contains other strings (prefix and suffix)

01: Check whether the string starts with another string-(BOOL) hasPrefix: (NSString *) aString;
NSString * String1 = @ "NSStringInformation.txt ";
[String1 hasPrefix: @ "NSString"] = 1? NSLog (@ "YES"): NSLog (@ "NO ");
[String1 hasSuffix: @ ". txt"] = 1? NSLog (@ "YES"): NSLog (@ "NO ");

02: Check whether a string contains another string-(nsange) rangeOfString: (NSString *) aString. This is previously used to search for substrings in the string;


Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send

Game official download: http://dwz.cn/RwTjl

Game video preview: http://dwz.cn/RzHHd

Game Development blog: http://dwz.cn/RzJzI

Game source code transfer: http://dwz.cn/Nret1


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.