Swift Basic Syntax: string article

Source: Internet
Author: User



First, let's review the string-related knowledge in C and OC:



In C, it is generally said that the string is an ordinary character array, such as char characters[12] = "Hello world", whereas the reference string in C language is usually related to the pointer, such as char * s = "Hello World", both of which One obvious difference is that the characters in the characters exist in the stack, and the string that s points to is present in the constant area, which needs attention;



When we are dealing with the strings in C, we usually refer to the library files given by the system <string.h>, and compare, copy, paste, etc. directly using some library functions to achieve the corresponding effect;



In the OC language, string types are divided into NSString and nsmutablestring, which can be understood as mutable and immutable strings, so the name Incredibles, for mutable strings We can add, delete, and so on in the source string, and for the immutable string, the contents of the modification, will produce a new string;



String in Swift:



First of all, for the strings in swift, we need to distinguish between the characters in Swift and the strings in the relationship and the difference: character and string;



As follows:








None of the two statements have their type, and because of the action of the left derivation type of swift, the default temp is character (character) type, and Othertemp is a string (string) type;



Note: The values of string and character types are double quotation marks; character type, cannot append to itself string;



Let's look at some of the actions for string: (Here we no longer differentiate between Var and let)






1. Initialize string








Here the two initialization methods are initialized two empty strings;






2. Determine if the string is empty





Console printing information: The string is empty, here we can get the number of characters in the string by the function countelements ();






3, the concatenation of strings





Where: The 4th method, insert index position, type must be string.index type, cannot be int type, so we need to get the corresponding index by string before the insert operation succeeds;



In the 5th way, the string type in Swift is converted to the nsmutablestring type in OC, after which the usage is the same as the nsmutablestring in OC;



The final console printing effect is:








4. Remove elements





In this, you need to pay attention to the code of 2 and 2, the advance function The second parameter position, the positive number represents the backward (right) move position, the negative number represents the forward (left) move position; startindex is the beginning of the current string, Endindex is the end of the current string StartIndex ... endindex range, note ... The space before and after the (closed interval) must be matched, otherwise the compiler will report an error: The parameter in the RemoveAll function is of type bool;



Take a look at the console output:








5. Query string





Swift query string includes three ways, the first type is hasprefix, query prefix, the second type is hassuffix, query suffix, the last is = = Double equals and! = equals, Comparison is equal, we will introduce the form of = = = three equals and!==;






6. Traversing a string





Here is a brief introduction to the two methods of traversal, other methods are less used, you can view the relevant development documents


















This article is from the "Talented Woman Kindafford" blog, please make sure to keep this source http://liwenyu.blog.51cto.com/9864907/1611032



Swift Basic Syntax: string article


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.