Swift Learning Notes (grammar)--string and character types

Source: Internet
Author: User

One: String overview

Unlike NSString and nsmutablestring in Objective-c, in swift, storing string data can change, depending on whether the definition data is VAR or let,var is a variable, can change the length of a string, add, modify, Delete Each of these characters, and let is a constant, and once defined, it is no longer possible to modify its contents.

Swift's string type is a value type, and if you create a new string type of data, the value is copied when it is assigned, resulting in a copy.

II: Character overview

The string is actually used as a collection of character types, and each of its character data can be obtained by traversing each element of the string.

In Swift, a character is represented by a double quotation mark, meaning that character is interpreted as a string of length equal to 1 in swift, and the character type must contain only one character.

The character (Character) type must be represented by double quotation marks, if the compilation error is reported in single quotation marks

Let Exclamation:character = "!"

You can also use the character array to create a string

Let catcharacter: [Character] = ["C", "a", "T", "!"] Let catstring = String (catcharacter)

Three: String operations

Let string1 = "Hello" let string2 = "There" var welcome = string1 + string2  //string concatenation, provided that all elements of the + operation must be guaranteed to be not an optional variable, must be a value of 
    
     string type, cannot be 
     String? Optional type, optional type not able to participate in + operation   
    
The insert operation of the string, in the form "test string \(varinstance)", must be guaranteed that varinstance is not an optional type, varinstance must have a definite value
var instruction = "Look over" instruction + = String2let Exclamation:character = "!" Welcome.append (exclamation) //Note string type and character type cannot use the + operator directly because inconsistent data types on both sides cause the compiler to error

  

Swift Learning Notes (grammar)--string and character types

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.