Common nsstring and nsmutablestring Methods

Source: Internet
Author: User

Nsstring Method
+ (ID) stringwithcontentsoffile: path encoding: ENC error: Err
Create a new string and set it to the content of the file specified by path. Use the character encoding enc. If it is not zero, an error in err is returned.
+ (ID) stringwithcontentsofurl: URL encoding: ENC error: Err
Create a new string and set it as the URL content. Use the character encoding enc. If it is not zero, an error in err is returned.
+ (ID) String
Create a new Null String
+ (ID) stringwithstring: nsstring
Create a new string and set it to nsstring
-(ID) initwithstring: nsstring
Set the allocated string to nsstring.
-(ID) initwithcontentsoffile: path encoding: ENC error: Err
Set the string to the content of the file specified by path.
-(ID) initwithcontentsofurl: URL encoding: ENC error: Err
Sets the string to the content of the URL (nsurl *) URL and uses the character encoding enc. If it is not zero, an error in err is returned.
-(ID) (unsigned INT) Length
Returns the number of characters in a string.
-(Unichar) characteratindex: I
Returns the Unicode Character of index I.
-(Nsstring *) substringfromindex: I
Returns the substring starting with I that knows the end of the string.
-(Nsstring *) substringwithrange: Range
Returns a substring based on a specified range.
-(Nsstring *) substringtoindex: I
Returns the substring starting from this string to suo I.
-(Nscomparator *) caseinsensitivecompare: nsstring
Compare two strings, case insensitive
-(Nscomparator *) compare: nsstring
Compare two strings
-(Bool) hasprefix: nsstring
Test whether the string starts with nsstring
-(Bool) hassuffix: nsstring
Test whether the string ends with nsstrng
-(Bool) isw.tostring: nsstring
Test whether two strings are equal
-(Nsstring *) capitalizedstring
Returns a string of the upper-case letters of each word (the other letters of each word are converted to lower-case letters)
-(Nsstring *) lowercasestring
Returns a string converted to lowercase.
-(Nsstring *) uppercasestring
Returns a string converted to uppercase.
-(Const char *) utf8string
Returns a string that is converted to a UIF-8 string.
-(Double) doublevalue
Returns the string converted to double.
-(Float) floatvalue
Returns a string converted to a floating point value.
-(Nsinteger) integervalue
Returns a string converted to an nsinteger.
-(INT) intvalue
Returns the string converted to an integer.

Nsmutablestring Method
+ (ID) stringwithcapacity: Size
Creates a string that contains characters of size.
-(ID) initwithcapacity: Size
Use a string with the initial capacity of size to initialize the string
-(Void) setstring: nsstring
Set the string to nsstring
-(Void) appendstring: nsstring
Append an nsstring to the end of the receiver
-(Void) deletecharactersinrange: Range
Delete characters in a specified range
-(Void) insertstring: nsstring atindex: I
Insert nsstring at the starting position of index I
-(Void) replacecharactersinrange: range withstring: nsstring
Use nsstring to replace the character specified by range
-(Void) replaceoccurrencesof
String: nsstring withstring:
Nsstring2 options: opts range: Range

According to opts. Replace all nsstrings with nsstring2 in the specified range. Options can include nsbackwardssearch (search from the end of the range) nsanchoredsearch (nsstring must match the start of the range), nsliteralsearch (execute byte comparison and aceinsensitivesearch by bit or combination)




Nsmutablestring

[Astring substringtoindex :( [astring length]-1)]; // Delete the last character from the string

// Delete the last character from the string

Nsange range = {0, 1 };

[Astr deletecharactersinrange: range];


Nsstring is immutable, meaning that the declared object cannot be changed. If you want to change it, you can use its subclass: nsmutablestring

 

You can use the class method stringwithcapacity to create a new nsmutablestring. The declaration is as follows:

* (ID) stringwithcapacity: (unsigned) capacity:

 

You can declare a new variable string as follows:

Nsstring * str1;

Str1 = [nsmutablestring stringwithcapacity: 42];

 

You can use appendstring or appendformat to operate a variable string:

-(Void) appendstring: (nsstring *) astring;

-(Void) appendformat: (nsstring *) format;

 

Appendstring accepts the astring parameter and then copies it to the end of the receiving object.

Appendformat is similar. It attaches the formatted string to the end of the accepted object, rather than creating a new object.

Exp:

Nsmutablestring * string;

String = [nsmutablestring stringwithcapacity: 50];

[String appendstring: @ "hello, there"];

[String appendformat: @ "human % d !", 39];

The string value of this Code is finally assigned: Hello, there human 39!

 

You can also use deletecharactersinrange: To delete characters in a string:

-(Void) deletecharactersinrange: (nsange) range;

 

Deletecharactersinrange: And rangeofstring: are usually used together. nsmutablestring can use all functions of nsstring, including rangeofstring:, string comparison, and any other functions.

For example, if you have listed a list of all your friends and suddenly do not like Jack, you want to delete him from the list of friends:

// Friend list Initialization

Nsmutablestring * friends;

Friends = [nsmutablestring stringwithcapacity: 50];

[Friends appendstring: @ "level Fang Jack even"];

 

// Find Jack and delete it

Nsange jackrange;

Jackrange = [friends rangeofstring: @ "Jack"];

Jackrange. Length ++; // eat the space that follows

[Friends deletecharactersinrange: jackrange];

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.