NSString type-learning summary,

Source: Internet
Author: User

NSString type-learning summary,
1. Create a string (1) create a constant string

NSString * str = @ "This is a String"; // str is the variable name.

(2) create an empty string and assign a value to the string

NSString * str = [NSString alloc] init];

Str = @ "Kobe Bryant"; // assign a value directly

(3) pattern assignment

<1> initialization method: initWithFormat: initializes string objects based on a certain string format, and writes content in the format string into the string object space.

NSString * str = [[NSStringalloc] initWithFormat: @ "iphon % @", @ "e6"];

<2> use the convenient constringwithformat: to quickly create objects, perform alloc and initialization operations internally, and initialize initWithFormat initialization Methods internally.

NSString * str = [NSString stringWithFormat: @ "Funny ratio % @", @ "er"];

<3> Create a string using standard C Language

Char * name = "say hello ";

NSString * sayHello = [[NSString alloc] initwithuf8string: name];

2. Obtain the length of a string (Keyword: length)

NSString * str = @ "You Know What You Think ";

NSUInteger length = str. length; // or [str length], the length can also be an attribute in OC and can be called directly.

NSLog (@ "% ld", length );

3. String concatenation (Key Words: append)

NSString * str1 = @ "podouye Jieyi ";

NSString * str2 = @ "AV female ";

Method 1:

NSString * str3 = [str1 stringByAppendingString: str2]; // because NSString is an immutable string, only new strings can be returned, and the meta string remains unchanged.

Method 2

NSString * str4 = [str1 stringByAppendingFormat: @ "% @ % @", @ "18," @ ""];

4. String truncation (Key Words: sub)

NSString * str = @ "say goodbye ";

<1> starting from the specified position (including the characters at the specified position), all the characters after the truncation

NSString * str1 = [str substringFromIndex: 3];

<2> truncates a string from the beginning to the specified position, but does not include any character of this position.

NSString * str2 = [str substringToIndex: 3];

<3> extract the substring from the string according to the given position and length.

NSString * str3 = [str substringWithRange: NSMakeRange (2, 2)]; // NSMakeRange quick construction of nsange (subscript, length)

5. String case-insensitive Conversion

NSString * str = @ "xiAo m/eI mei ";

NSLog (@ "% @", [str uppercaseString]); // change all to uppercase

NSLog (@ "% @", [str lowercaseString]); // change all to lowercase.

NSLog (@ "% @", [str capitalizedString]); // uppercase for the first character, including space and character disconnection, or uppercase for the second character

6. String comparison

NSString * str1 = @ "hello boby ";

NSString * str2 = @ "hello dowhat ";

<1> compare the string address

BOOL B = (str1 = str2); // The memory address of two strings

NSLog (@ "% d", B );

<2> whether the values of the two strings are equal

BOOL B = [str1 isw.tostring: str2]; // if the content of the two strings is the same, a BOOL type value is returned.

NSLog (@ "% d", B );

<3> compare the string size

NSComparisonResult result = [str1 compare: str2]; // compare the result by letter and set the return value of an enumerated type (NSOrderedSame NSOrderedAscending NSOrderedDescending)

Switch (result ){

Case NSOrderedSame:

NSLog (@ "same ");

Break;

Case NSOrderedAscending:

NSLog (@ "first small and later ");

Break;

Case NSOrderedDescending:

NSLog (@ "before and after effect ");

Break;

Default:

Break;

}

<4> determine whether a string starts or ends with a certain character (keyword has)

BOOL b3 = [str1 hasPrefix: @ "boby"]; // judge the string prefix.

BOOL b4 = [str2 hasSuffix: str1]; // judge the string suffix

<5> check whether a string contains a certain character.

Nsange range = [str rangeOfString: @ "hi"];

If (range. location! = NSNotFound ){

}

Else {

}

7. Copy strings

NSString * str8 = [NSString stringWithString: str3]; // The string attributes follow the copying protocol.

NSLog (@ "% @", str8 );

8. replacing)

NSString * string1 = @ "abcdefg", * string2 = @ "higkllmn ";

NSString * str1 = [string1 stringByReplacingCharactersInRange: NSMakeRange (2, 3) withString: string2]; // extract several strings from the middle and replace them with the characters After withString

NSString * str2 = [string1 stringByReplacingOccurrencesOfString: @ "a" withString: @ "ABV"]; // replace all the characters in the string "a" with "ABV"

 

9. Variable string type

NSString is an immutable string and cannot delete or add characters (the above operation will open up a new string to save the added or replaced string, the original string will not change ).

Cocoa provides a subclass of NSString: NSMutableString. Because it is a subclass of NSString, NSMutableString inherits all NSString methods.

When we create a variable string (or all the variable arrays, dictionaries, collections, and so on), we must first initialize and then use it.

<1> Initialization Method

NSMutableString * mutablestring = [NSMutableString string]; // class method initialization (rarely used)

NSMutableString * mstr1 = [[NSMutableString alloc] init]; // init instance method Initialization

NSMutableString * mstr2 = [[NSMutableString alloc] initWithCapacity: 0]; // specify the initialization method. capacity is used to apply for memory space, however, the middleware will automatically adjust the memory size based on the increase or decrease of the string (more projects are used)

<2> your own unique SDK

[Mstr1 setString: @ ""]; // assign values to or reset a string (replace the original content)

NSMutableString * mstr3 = [NSMutableString stringWithFormat: @ "Four is four, ten is ten, forty is forty"];

[Mstr3 insertString: @ "Tease ratio" atIndex: 1]; // insert a string at a location

[Mstr3 deleteCharactersInRange: NSMakeRange (3, 2)]; // delete a string within a certain range

NSMutableString * m1 = [NSMutableStringstringWithCapacity: 0];

NSMutableString * m2 = [NSMutableStringstringWithString: @ "Tom"];

[M1 appendString: @ "Haha"]; // assign a value to the string (OC is more flexible)

[M1 appendFormat: @ "age is % d", 12]; // insert a string

[M1 replaceCharactersInRange: NSMakeRange (3, 2) withString: @ "Tease ratio"]; // Replace the content in the Escape Character and select a position.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.