Common nsstring and nsmutablestring usage

Source: Internet
Author: User

Nsstring class is a frequently used class. Here we classify its usage and common methods.

1> Create a string

Nsstring * STR = @ "string content"; // directly use the String constant Assignment Method

Nsstring * STR = [nsstring alloc] init]; // create an empty string object

Nsstring * STR = [nsstring stringwithformat: @ ""]; // create a string using the formatting method of the Class Method

Nsstring * STR = [nsstring stringwithstring: @ ""]; // generate a string by copying an existing string.

Nsstring * STR = [nsstring alloc] initwithstring: @ ""]; // create a string using the instance method

Nsstring * STR = [nsstring alloc] initwithformat: @ ""]; // create a string using the format creation method of the instance method

There are other creation methods, such as converting a C string into an OC string.

 

2> string comparison

-(Nscomparisonresult) Compare :( nsstring *) string;

// Compare two strings and return an enumeration type. The enumerated type has three values-1: indicating ascending order 0: the two are equal. 1: indicates descending order.

-(Nscomparisonresult) Compare :( nsstring *) string options :( nsstringcompareoptions) mask;

// Compare two strings in the specified Method

**************************************** ********

The options parameter of the comparison method with restrictions is an enumeration type that has the following common conditions:

1> nscaseinsenstivesearch (1): case-insensitive comparison

2> nsliteralsearch (2): case-sensitive comparison

3> nsbackwardssearch (4): Start from the end

4> nsanchoredsearch (8): normal sequence comparison

5> nsnumericsearch (64): compares the number of characters in a string.

**************************************** ********

-(Nscomparisonresult) Compare :( nsstring *) string options :( nsstringcompareoptions) mask range :( nsange) comparerange;

// Select a range and use a comparison method with restrictions within the range * Note that the comparison method is to retrieve the string from the range of str1 and compare it with the entire str2.

-(Bool) isequaltostring :( nsstring *) astring; // you can easily determine whether two strings are equal and return a bool value.

*-(Nscomparisonresult) localizedcompare :( nsstring *) string;

*-(Nscomparisonresult) localizedcaseinsensitivecompare :( nsstring *) string;

// The comparison method of the two localization rules is not clear yet. Note *

 

3> modifying strings

1) common case-sensitive Conversions

-(Nsstring *) uppercasestring; // replace all strings with uppercase letters.

-(Nsstring *) lowercasestring; // change all strings to lowercase letters.

-(Nsstring *) capitalizedstring; // change the first character of all words to uppercase.

2) retrieve the substring

-(Nsstring *) substringfromindex :( nsuinteger) from; // retrieves the character string containing the index position from the index mark position to the end

-(Nsstring *) substringtoindex :( nsuinteger) to; // retrieves the string from the beginning to the position marked by index, excluding the characters at the index position

-(Nsstring *) substringwithrange :( nsange) range; // retrieves the string within the range. Pay attention to the boundary.

3) String concatenation

-(Nsstring *) stringbyappendingstring :( nsstring *) astring; // concatenates one string to another and generates a new string.

-(Nsstring *) stringbyappendingformat :( nsstring *) format; // concatenates A formatted string to another string to generate a new string.

4) string replacement

-(Nsstring *) stringbyreplacingoccurrencesofstring :( nsstring *) Target withstring :( nsstring *) replacement;

// Check whether the string contains the following string and replace it with the specified string.

-(Nsstring *) stringbyreplacingcharactersinrange :( nsange) range withstring :( nsstring *) replacement;

// Replace the specified string with the specified string

-(Nsstring *) stringbyreplacingoccurrencesofstring :( nsstring *) Target withstring :( nsstring *) replacement options :( nsstringcompareoptions) Options range :( nsange) searchrange;

// Check whether the search within the specified range contains the following strings and replace them with the specified strings in options.

 

4> search strings

-(Nsange) rangeofstring :( nsstring *) astring; // locate a string in a string and return a range

-(Nsange) rangeofstring :( nsstring *) astring options :( nsstringcompareoptions) mask;

// Query the position of a string in a specified way and return a range

-(Nsange) rangeofstring :( nsstring *) astring options :( nsstringcompareoptions) mask range :( nsange) searchrange;

// In the specified range, locate the position of a string in the specified way, and return a range

-(Bool) hasprefix :( nsstring *) astring; // determines whether it starts with a string.

-(Bool) hassuffix :( nsstring *) astring; // determines whether it ends with a string.

 

5> reading and writing strings and other methods

-(Nsarray *) componentsseparatedbystring :( nsstring *) separator;

// Split the string into a string, store the string into a string array, and return the Array

-(Nsarray *) componentsseparatedbycharactersinset :( nscharacterset *) separator;

// Split the string into characters in a set, store the string into the string array, and return the Array

-(Nsuinteger) length; // returns the string length.

-(Unichar) characteratindex :( nsuinteger) index; // return the character marked by index

-(Double) doublevalue; // convert a string of pure numbers to the double type.

-(Float) floatvalue; // convert a string of pure numbers into a float type

-(INT) intvalue; // convert a string of pure numbers to the int type.

-(Bool) writetourl :( nsurl *) URL atomically :( bool) useauxiliaryfile encoding :( nsstringencoding) ENC error :( nserror **) error;

// Write the string to the URL. If yes is returned

-(Bool) writetofile :( nsstring *) path atomically :( bool) useauxiliaryfile encoding :( nsstringencoding) ENC error :( nserror **) error;

// Write the string to the specified path. If yes, yes is returned.

+ (Instancetype) stringwithcontentsofurl :( nsurl *) URL encoding :( nsstringencoding) ENC error :( nserror **) error;

// Write the content of the specified URL to the string,

+ (Instancetype) stringwithcontentsoffile :( nsstring *) path encoding :( nsstringencoding) ENC error :( nserror **) error;

// Write the file content in the specified path to the string

 

As a subclass of nsstring, nsmutablestring inherits most of the methods and has some unique methods.

1> Create nsmutablestring

-(ID) initwithcapacity :( nsuinteger) capacity; // instance method. initialize a variable string with the capacity length.

+ (ID) stringwithcapacity :( nsuinteger) capacity; // class method. initialize a variable string with the capacity length.

2> nsmutablestring Modification

-(Void) setstring :( nsstring *) astring; // change the content of an existing variable string to another string

-(Void) insertstring :( nsstring *) astring atindex :( nsuinteger) loc; // insert a string at the position marked by the index

-(Void) deletecharactersinrange :( nsange) range; // delete a string within the specified range

-(Void) appendstring :( nsstring *) astring; // Concatenates the string to an existing string.

-(Void) appendformat :( nsstring *) format; // Concatenates the formatted string to an existing string.

 

The modification method of nsstring and nsmutablestring shows the obvious difference. The nsstring modification method returns an nsstring object, because a new String object must be used to save the modified string. The nsmutablestring modification method returns a null value because it is modified on the original variable string, and the modified string is still the address of the original string.

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.