Objective-C diary ③ string

Source: Internet
Author: User
1. Create strings and class methods

Create an nsstring using the formula

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

Eg:

Nsstring * height;

Height = [nsstring stringwithformat: @ "height: % d length: % d", 10, 20];

The resulting string: "height: 10 length: 20"

 

Note:

Ellipsis: It indicates that multiple parameters can be received, similar to nslog ();

Plus sign: "+" when a class is generated during object-C runtime,Class Object(Class Object includes: pointer to the superclass, class name, pointer to the class method List)

How to add a plus sign when declaring a method, this method isClass Method.This method is a Class Object (rather than an Instance Object of the class) and is often used to create a new instance. We call this class method to create a new objectFactory method(StringwithformatIs a factory method)

 

If this method runs in an object instance, it uses the leading minus sign (-) to start the declaration.

If you create an instance object or access some global class data, we recommend that you use the leading plus sign (+) to declare it as a class method.

Ii. Comparison of size and string

About size:

Nsstring contains a convenient method of length, which returns the number of characters in the string

Usage:Unsigned int length = [height length];

Note: The length method of nsstring can accurately process international strings. For example, Chinese characters and Unicode international character standard strings are different from the C language. The C language may occupy more than one character.

 

String comparison:

A: isequaltostring: Compares the receiver and string passed as a parameter. Returns a bool (Yes or no is not true, false) To indicate whether the string content is the same. Format:-(Bool) isequaltostring:(Nsstring *) astring;

Eg:

Nsstring * str1 = @"  Hello Pepe  "  ; Nsstring * Str2 = [nsstring stringwithformat: @" Hello % s  " , "  Pepe  "  ];  If  ([Str1 isw.tostring: str2]) {nslog (  @"  Same  "  );}  Else  {Nslog (  @"  Different  " );} 

The result is of course "same"

B: CompareThe method is declared as follows:-(Nscomparisonresult) compare:(Nsstring *) String

Compare compares the received object with the passed string one by one, and returns an nscomparisonresult (Enum data) to display the comparison result.

Eg:

Bool result = [ @"  Kaka  " Compare:@"  Pepe  "  ]; Nslog (  @"  % S  " , "  KaKa vs Pepe  "  ); Nslog (  @"  % D  "  , Result); Result = [ @"  Kaka " Compare: @"  Kaka  "  ]; Nslog (  @"  % S  " , "  KaKa vs Kaka  "  ); Nslog (  @"  % D  "  , Result); Result = [@"  Pepe  " Compare: @"  Kaka  "  ]; Nslog (  @"  % S  " , "  Pepe vs Kaka  "  ); Nslog (  @"  % D  " , Result );

Result:

Note:

Use isequaltostring to compare whether two strings are equal:Instead of comparing the pointer values of strings;

Eg:

If ([str1 is1_tostring: str2]) {Nslog (@ "same ");}Vs if (str1 = str2) {nslog (@ "same ");}

Note the differences between the two: 1. = Operator only determines the pointer values of str1 and str2, rather than the objects they refer

2. If you want to check the identifiers of two objects, you can use =

3. If you want to view the equivalence, use isequaltostring


Compare compares the following types of results returned by a string:

 

C: case-insensitive comparison

-(Nscomparisonresult) compare: (nsstring *) string options: (unsigned) mask;[K branch m' P branch RIS branch N] comparison; metaphor; Comparison

The options parameter is a bitmask. You can use bitwise OR operator (|) to add the option Tag:

Nscaseinsensitivesearch: case-insensitive comparison;[In 'sens when Tiv... No feeling

Nsliteralsearch: complete comparison, case sensitive;['Invalid employee R limit L]The words are in words.

Nsnumericsearch:Compares the number of characters in a string, not the character value;[Nju: 'merik] Number; Numeric value (equal to mumerical); Numeric Value

 

Eg:

 

 

D: whether the string contains other strings

Check whether the header and tail contain a certain string:Hasprefix,Hassuffix

-(Bool) hasprefix :( nsstring *) string; check whether the string starts with a certain string.[, PRI: 'fiks, 'pri: fiks] With a prefix. add something to the front.

-(Bool) hassuffix: (nsstring *) string; check whether the string ends with a certain string.['S token fiks, s token 'fiks] vt. suffix N. [language] suffix; subscript

Eg:

Query whether a string contains a specific string: rangeofstring returns a nsange struct that tells you where the matching part of the string is and the number of matched characters.

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

LocationUsed to store the starting position of the field range

LengthIs the number of elements contained in the range

Eg:

 

Iii. Variability

Nsstring is an immutable string. To use a variable string, cocoa provides an nsstring subclass nsmutablestring.

Format

-(ID) stringwithcapacity :( unsigned) capacity;

You can use appendstring and appendformat to add a new string:-(void) appendstring :( nsstring *) string;-(void) appendformat :( nsstring *) format ......;

Note:

Appendstring receives the string parameter and copies it to the end of the string object.

Appendformat is similar to stringwithformat, But it attaches the formatted string to the end of the received string, rather than creating a new String object;

Eg:

Use the deletecharactersinrange method to delete characters in a string:

-(Void) deletecharactersinrange :( nsange) range;

Because nsmutablestring is a subclass of nsstring, The nsmutablestring method of nsstring can be used.

Eg:

 

 

 

 

 

 

 

 

 

 

 

 

 

author: Pepe
Source: http://pepe.cnblogs.com/
the copyright of this article is shared by the author and the blog Park. You are welcome to reprint it, but you must keep this statement without the author's consent, in Chapter the original text link is clearly displayed on the page. Otherwise, the legal liability is reserved.

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.