What about nsstring in objective-C syntax (3)

Source: Internet
Author: User
Tags truncated
What are the strings in objective-C syntax?

If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/381

In objective-C, nsstring and nsmutablestring are core classes that process strings. The biggest difference between these two classes is that after nsstring is created and assigned a value, the content and length of the string cannot be dynamically changed, unless this string is assigned again. The content and length of the nsmutablestring can be changed dynamically after being assigned a value.

1. Create a classic nsstring
The biggest difference between nsstring and char * is that nsstring is an objective object, while char * is a byte array. @ + "String" indicates the standard usage of the objective-C nsstring String constant. You do not need to add @ when creating a char @

-(Void) viewdidload {[Super viewdidload]; // classic string value assignment nsstring * str0 = @ "I am Yu Song Momo "; // string formatting and merging include // nsstring * type int type char * type nsstring * str1 = [nsstring stringwithformat: @ "my name: % @ My age: % d my mailbox: % s ", @" Yu Song Momo ", 25," xuanyusong@gmail.com "]; // only one string can be written in the string assignment parameter, which is similar to nsstring * str2 = [nsstring stringwithstring: @ "I Am a string"]; // The string to UTF-8 format parameter is char * type nsstring * str3 = [nsstring stringwithuf8string: "string to UTF-8 format"]; // string to merge int I = 100; char * c = "xuanyusong"; nsstring * temp = @ "I Am a temporary string "; // Add int I and char * C to form a new string nsstring * str4 = [temp stringbyappendingformat: @ "INTEGER: % d character type: % s ", i, C]; // Add temp Based on the string temp and form a new string nsstring * str5 = [temp stringbyappendingstring: temp]; // string output nslog (@ "str0 = % @", str0); nslog (@ "str1 = % @", str1); nslog (@ "str2 = % @", str2); nslog (@ "str3 = % @", str3); nslog (@ "str4 = % @", str4); nslog (@ "str5 = % @", str5 );}



2. String Traversal
Each string is actually composed of several char characters. The traversal of a string is actually to extract each character from the string.

-(Void) viewdidload {[Super viewdidload]; // classic string value assignment nsstring * STR = @ "yusongmomo"; // String Length int COUNT = [STR length]; nslog (@ "the string length is % d", count); // traverse each character in the string for (INT I = 0; I <count; I ++) {char c = [STR characteratindex: I]; nslog (@ "the character string % d is % C", I, c );}}



3. String comparison
Whether the isequaltostring compares strings are completely equal, and cannot be completely matched if the case is different. Hasprefixe matches string Headers Hasuffix matches the string's tail
-(Void) viewdidload {[Super viewdidload]; nsstring * str0 = @ "Yu Song Momo"; nsstring * str1 = @ "Yu Song Momo "; // If ([str0 isequaltostring: str1]) {nslog (@ "string is completely equal");} // If ([str0 hasprefix: @ "Yu Song"]) {nslog (@ "string str0 starts with Yu Song");} // string comparison if ([str1 hassuffix: @ "momo"]) {nslog (@ "str1 string ending with Momo ");}}



4. String truncation and Case sensitivity change
-(Void) viewdidload {[Super viewdidload]; nsstring * str0 = @ "Chinese my name is xuanyusong "; // content nsstring * To = [str0 substringtoindex: 4]; nslog (@ "to = % @", ); // The content of the truncated Character index between 2 and the end nsstring * From = [str0 substringfromindex: 2]; nslog (@ "from = % @", from ); // set the range of the string to be truncated // from the second to the tenth, nsange rang = nsmakerange (2, 10); nsstring * strrang = [str0 substringwithrange: rang]; nslog (@ "rang = % @", strrang); // set the upper-case nslog (@ "str0 upper-case: % @", [str0 capitalizedstring]); // set all strings to uppercase nslog (@ "str0 uppercase: % @", [str0 uppercasestring]); // set all characters to lowercase nslog (@ "str0 lowercase: % @ ", [str0 lowercasestring]);}



5. Search for and replace strings
-(Void) viewdidload {[Super viewdidload]; nsstring * str0 = @ "Chinese my name is xuanyusong"; nsstring * temp = @ "is"; nsange rang = [str0 rangeofstring: temp]; nslog (@ "index of the start point of the searched string in str0 is % d", rang. location); nslog (@ "the index of the string to be searched in str0 is % d", rang. location + rang. length); // Replace the string in the search with a new string nsstring * STR = [str0 stringbyreplacingcharactersinrange: rang withstring: @ ""]; nslog (@ "Replace the string with % @", STR); // Replace "" in the string with * STR = [str0 stringbyreplacingoccurrencesofstring: @ "" withstring: @ "@"]; nslog (@ "after replacement, the string is % @", STR );}

Extended: You can use the following method to replace the entire string and set the region to be replaced.

ONS: <# (nsstringcompareoptions) #> range: <# (nsange) #>

Stringbyreplacingoccurrencesofstring :( nsstring *) withstring :( nsstring *) Options :( nsstringcompareoptions) range :( nsange)





This article mainly introduces the use of the nsstring class. The following is a detailed introduction of the nsmutablestring class. Large and small basin friends should support Momo ~~~


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.