Nsstring of objective-C

Source: Internet
Author: User
Document directory
  • Create a string
  • Class Method
  • Get size
  • Comparison Policy
  • Case-insensitive comparison
  • How to determine whether a string contains other strings

Nsstring is a class used in cocoa to process strings. Below are some common nsstring methods and operations:

Create a string

NsstringStringwithformat: Create an nsstring using the format string and parameters:

+ (ID) stringwithformat: (nsstring *) format ,...;

You can use it as follows, for example:

Nsstring * height;

Height = [nsstring stringwithformat: @ "Your heightis % d feet", 5];

The resulting string is "yourheight is 5 feet"

Class Method

I have seen the definition of stringwithformat in the previous section. I do not know whether it is correct. There are two major differences between stringwithformat and the method we previously declared:

The first is to end the parameter with ",...", which means it can be connected to multiple parameters;

The second is that the method is not "-", but "+". What does this mean? This indicates that it isClass MethodThis method is a Class Object (rather than an Instance Object) and is usually used to create a new instance. The class method used to create a new object is calledFactory method.

 

Get size

Another instance method in nsstring isLengthReturns the number of characters in the string.

-(Unsigned INT) length;

You can use it like this:

Unsigned int Len = [height length];

 

Comparison Policy

Isequaltostring: Can be used to compare the recipient and the symbol passed as a parameter. It returns a boolean data to indicate whether the content of the two strings is the same. The statement is as follows:

-(Bool) isw.tostring: (nsstring *) astring;

Example:

Nsstring * string1 = @ "Hello 5 ";

Nsstring * string2;

String2 = [nsstring stringwithformat: @ "Hello % d", 5];

If ([string1 isw.tostring: string2])

{

Nslog (@ "they are same! ");

}

 

Compare: used to compare two strings, compare the received object with the passed string, and return an nscomparisonresult (an Enum type data) to display the comparison result. The declaration is as follows:

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

The return value is defined as follows:

Typedef Enum _ nscomparisonresult {
Nsorderedascending =-1, // <ascending
Nsorderedsame, // = equal
Nsordereddescending //> descending order
} Nscomparisonresult;

NOTE: If two strings are equal, use isequaltostring instead of just comparing the string pointer value. In addition, compare can only be judged based on the first character, for example, 100 and 90, that is, ascending. abcdef is compared with DC, which is also ascending.

 

Case-insensitive comparison

Compare: You can also perform case-sensitive comparisons. It also has a type of overload, with an option parameter added to give us more choices and operations. The specific method is defined as follows:

-(Nscomparisonresult) Compare :( nsstring *) String

Option :( unsigned) mask;

 

The options parameter is a bitmask. You can use bitwise OR operators to add option tags. Common options are as follows:

Nscaseinsensitivesearch: case-insensitive characters.

Nsliteralsearch: complete comparison, case sensitive.

Nsnumericsearch: compares the number of characters of the character Lu, not the character value.

For example, if you want to compare strings, you need to ignore the case, but sort the strings correctly by the number of characters, you should use the following:

If ([thing1compare: thing2

Option: nscaseinsensitivesearch

| Nsnumericsearch]

= Nsorderedsame)

{

Nslog (@ "they match !");

}

 

How to determine whether a string contains other strings

For example, you need to determine whether a file name contains ". avi, to determine whether the player can be used to open it, or you want to check whether the start of a file contains a character "my" to determine whether it is your document. There are two methods to help you determine: one is to check whether the string starts with another string, and the other is to check whether the string ends with another string:

-(Bool) hasprefix :( nsstring *) astring;

-(Bool) hassuffix :( nsstring *) astring;

So how can we determine whether the string contains another string? There is another function:

-(Nsange) rangeofstring :( nsstring *) astring;

Returns the nsange struct, where range. Start is the position where it appears, and range. length is the length of the string to be compared. If it is not found, range. Start is equal to nsnotfound.

Variable string

Nsstring is immutable and cannot be changed after it is created. We can only perform operations on it without affecting its content, such as deleting a character or adding a character to change it. Therefore, cocoa provides an nsstring subclass calledNsmutablestringIf the defined string is changed, use this subclass.

UsageStringwithcapacity: Create a New nsmutablestring. The declaration is as follows:

-(ID) stringwithcapacity: (unsigned) capacity:

Note: capacity is only an optimal capacity. After this size is defined, it is not limited to this size.

Nsmutablestring * string;

String = [nsmutablestring stringwithcapacity: 42];

Common nsmutablestring operations are as follows:

Appendstring:AndAppendformat:To append a new string. The definition is as follows:

-(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.

Deletecharacterinrange: Used to delete characters in a string, defined as follows:

-(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.

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.