Immutable string NSString and variable string NSMutableString, nsstring replace string

Source: Internet
Author: User

Immutable string NSString and variable string NSMutableString, nsstring replace string

Immutable string

// Create a String object

NSString * string = [[NSString alloc] initWithFormat: @ "zhong huang-"];

NSString * string1 = [[NSString alloc] initWithFormat: @ "zhonger's age is % d", 33];

NSString * string2 = [[NSString alloc] initWithFormat: @ "zhonger's gender is % @", @ "ominous"];

NSString * string3 = @ "I love iOS ";

// Obtain the length of a string

Unsigned long length = [string2 length];

// Obtain characters in the string

Unichar character = [string2 characterAtIndex: 21];

// Compare the content of two strings

BOOL isEqual = [string3 isw.tostring: string4];

// String comparison

NSComparisonResult result = [string3 compare: string4];

// Truncate the string:

// 1. substringFromIndex:

// Capture a subscript until the end.

NSString * newString = [string substringFromIndex: 5];

// 2. substringToIndex:

// Capture data from the beginning to the end of a logo

NSString * newString1 = [string substringToIndex: 5];

// 3. substringWithRange:

// Extract a few characters from the beginning and 3 from the second.

// NSMakeRange select several locations from a subscript

// NSMakeRange (<# NSUInteger loc #>, <# NSUInteger len #>)

NSString * newString2 = [string substringWithRange: NSMakeRange (2, 3)];

// Concatenate the string stringByAppendingFormat

NSString * newStr = [string stringByAppendingFormat: @ ""]; // You can also print placeholders.

NSString * newStr1 = [string stringByAppendingFormat: @ "Force % d", 2];

NSString * newStr2 = [string stringByAppendingString: @ "Force 2"];

NSString * newStr3 = [string stringByAppendingString: string1]; // The placeholder cannot be printed...

// Replace the string.

NSString * news = [string2 stringByReplacingCharactersInRange: NSMakeRange (20, 2) withString: @ "android"];

// Use this parameter when you do not know the specific position of the replace. If there are multiple replicas, replace them all.

NSString * news1 = [string2 stringByReplacingOccurrencesOfString: @ "ominous" withString: @ "female and female"];

// Convert string to int type

NSString * numberStr = @ "123 ";

Int number = [numberStr intValue];

// Uppercase and lowercase strings and uppercase letters

NSString * resultStr = [string uppercaseString];

NSString * resultStr1 = [string lowercaseString];

NSString * resultStr2 = [string capitalizedString];

// Determine whether a string starts with the specified method or how it ends.

BOOL isTrue = [string hasPrefix: @ "zh"];

BOOL isTrue1 = [string hasSuffix: @ "z"];

 

Variable string

// Create a variable string with a capacity of 0. The system will automatically apply for Space Based on the content stored in the variable string.

NSMutableString * mStr = [[NSMutableString alloc] initWithCapacity: 0];

// Set content

[MStr setString: @ "the power of the flood cannot be blocked, and the Qilin force"];

// Splicing

[MStr appendFormat: @ ", it's amazing! "];

// Delete characters in a certain range

[MStr deleteCharactersInRange: NSMakeRange (6, 2)];

// Insert a specified string starting with a subscript

[MStr insertString: @ "中 "atIndex: 2];

// Replace a string within a certain range

[MStr replaceCharactersInRange: NSMakeRange (8, 2) withString: @ "Swallow"];

NSLog (@ "mStr is % @", mStr );

Note that the use of methods is a string, so you must remember the method name. If you do not know, you can read the api documentation. The api documentation provides a detailed description of all methods.

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.