Welcome to Swift (initial translation and annotations of Apple's official Swift document 18)---116~122 page (chapter II)

Source: Internet
Author: User
Tags scalar

Strings and characters (strings and characters)

A string is a sequence of characters, such as "Hello, World," or "Albatross." In swift, strings are declared using a string type, which represents a sequence of values consisting of a set of character (character) types.

The string type and the character type provide a quick way to process (Unicode-compatible) text in code. The syntax for string creation and management is similar to the C language, both lightweight and readable. Concatenation of strings is simply the use of the plus (+) operator, And the changes in the string can be processed according to constants or variables.

Although the syntax is simple, Swift's string type provides a modern, fast handling of characters. Each string is made up of Unicode characters and can access these characters.

constants, variables, literals, and expressions can also be inserted into a string. This makes it easier to create personalized strings to display, store, and maintain.

Note the point:

The sting type in Swift is bridged seamlessly with the NSString class of the foundation in OC. If you are using cocoa or cocoa Touch's foundation framework, all calls NSString The API can use string values. You can also use the string type value to match the API of any NSString instance.

String literals (string literal)

In your code, you can include a predefined string value (string literal), which is a sequence of fixed text characters enclosed in a set of double quotation marks ("").

String literals can be initialized with a constant or variable:

Let somestring = "Some string literal value

Because a string literal is used to initialize a constant somestring, Swift can check out the type of the constant.

The string literal can contain the following special characters:

    • Escaped special characters (null character), \ \ (backslash), \ t (horizontal tab), \ n (newline), \ r (carriage return), \ "(double quotes) and \ ' (single quote)
    • Single-byte Unicode notation, written as \xnn, where nn is two hexadecimal digits
    • A double-byte Unicode symbol, written as \unnnn, where nnnn is a four hexadecimal digit
    • Four-byte Unicode symbol, written as \unnnnnnnn, where nnnnnnnn is eight hexadecimal digits

The following code shows a sample of each special character. The constant wisewords contains two escaped characters Fu Di quotation marks ("), constants Dollarsign, Blackheart, and Sparklingheart demonstrate other different Unicode character formatting:

Let wisewords = "\" Imagination are more important than knowledge\ "-Einstein"

"Imagination is more important than knowledge"-Einstein

Let dollarsign = "\x24"//$, Unicode scalar u+0024

Let Blackheart = "\u2665"//♥, Unicode scalar u+2665

Let Sparklingheart = "\u0001f496"//??, Unicode scalar u+1f496

Initializing an empty string (initializes a single null)

Creating a null-valued string can be done in two ways: assigning an empty text string to a variable, or initializing a new string instance with the initialization syntax:

var emptystring = ""//null character text

var anotheremptystring = String ()//initialization syntax

These strings is both empty, and is equivalent to all other

Determine whether the value of a string is empty by looking at their Boolean property IsEmpty:

If Emptystring.isempty {

println (" nothing to see here")

}

Prints "Nothing"

String mutability (variable string)

If you want a specified string to be modified (or changeable), you can assign it to a variable, and if you want the string to not be changed, assign it to a constant:

var variablestring = "Horse"

Variablestring + = "and carriage"

Variablestring is now "Horse and carriage"

Let constantstring = "Highlander"

Constantstring + = "and another Highlander"

This reports a Compile-time ERROR-A constant string cannot is modified "

Note the point:

This differs from the variable string of OC and cocoa, where a string in OC is variable and is selected between the two classes (NSString and nsmutablestring)

Strings is value Types (string literal value type)/*value type: The author's self-translation: This value type, please note when reference! may be wrong */

In Swift, a string type is a type of this value. If you create a new string value, when you pass it to a function or method, or assign it to a variable or constant, a copy of the string value is created. and the transfer and assignment are all using this copy, not the original value.

Note the point:

This behavior of strings in Swift is different from the way cocoa is nsstring. The NSString instance created in cocoa, passed to a function or method, or assigned to a variable, is a reference to the NSString, not a copy of the NSString (unless you explicitly specify that a copy is required).

The default copy (Copy-by-default) string for Swift is to ensure that when a method or function passes you a string value, you do not have to worry about where the value is coming from, and you can keep the string exactly (unless you modify it yourself).

In the background, Swift's compiler automatically chooses the use of strings, which guarantees that the copy-copy behavior of a string is performed only when it is necessary. This means that you can get very high efficiency when you use strings.

/******************** to be Continued ... ************************/

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.