Swift Language Guide (10) Swift language strings and characters

Source: Internet
Author: User
Tags constant scalar

A string is an ordered set of characters, such as "Hellow,world" or "Albatross." The strings in Swift are represented by string types and correspond to a collection of Character type values.

The String type in Swift provides a high-speed, Unicode-compliant text-handling style for your programming. Swift's syntax for creating and processing strings is light and readable, quite similar to the string syntax of the C language. The concatenation of strings is very simple, with just two strings added with the + operator. The value of a string depends on whether it is a constant or a variable, which is consistent with other types in Swift.

The string type of Swift, in addition to its syntax simplicity, is a high-speed, modern string implementation scheme. Each string is made up of Unicode characters encoded independently, each of which supports access in different Unicode representations.

Swift's string also supports inserting constants, variables, literals, and values of expressions in longer strings, known as String inserts. This makes it easier to display, store, and output custom string values.

Note:

Swift's String type seamlessly joins the NSString class at the bottom Foundation. If you use the Foundation framework in the Cocoa/cocoa touch, you can call all the APIs for the NSString class, in addition to the string attribute mentioned in this chapter, to any string value that you create. You can also pass a String value to any API method that requires an NSString instance.

For more information on the use of String in conjunction with the FOUNDATION/COCOA framework, see Swift's combination with Cocoa and objective-c (this part of the book is translated from the book).

Literal number of strings

In code, you can embed string literals (string literal) in a predefined string value. A string literal is a fixed sequence of text characters surrounded by a pair of double quotes ("").

String literals can provide an initial value for a constant or variable:

Let somestring = "Some string literal value"

Note that Swift infers that the constant somestring is of type string because the value of the somestring is initialized by a literal number of strings.

String literals cover the following special characters:

· Special characters escaped: (null character), \ (backslash, should be single slash--joe.huang after escaping), \ t (horizontal tab), \ n (newline), \ r (carriage return), \ "(double quotes) and \ ' (single quotes)

· Single-byte Unicode scalar, writing \xnn, where nn is two hexadecimal digits

· Double-byte Unicode scalar, writing \unnnn, where nnnn is four hexadecimal digits

· Four-byte Unicode scalar, writing \unnnnnnnn, where nnnnnnnn is eight hexadecimal digits

The following code shows examples of these special characters. The constant wisewords contains two escaped double quote characters. Constants Dollarsign, Blackheart, and Sparklingheart show three different writing formats for Unicode scalar characters:

Let wisewords = "\" Imagination are more important than knowledge\ "-Einstein"
"Imagination is more important than knowledge"-Einstein
Output "Imagination is more important than knowledge"-Albert Einstein
Let dollarsign = "\x24" //output $, Unicode scalar u+0024
Let Blackheart = "\u2665" /output, Unicode scalar u+2665
Let Sparklingheart = "\u0001f496" /output, Unicode scalar u+1f496

Initializes an empty string

To create a longer string, the first step is to create an empty string value that you can assign to a variable or initialize a new string instance with initialization syntax:

1 var emptystring = ""//empty string literal 2 var anotheremptystring = string ()//initialization Syntax 3//These two string objects are null values, equal to each other

You can use the IsEmpty property to detect whether the value of a string is null:

1 if Emptystring.isempty {2 println ("Nothing to read Here") 3} 4//Output "I don't see anything."

The variability of strings

Whether a value of a particular String can be modified (that is, mutable, mutable) can be assigned to a variable (which can be modified) or a constant (not modifiable) by declaring it:

var variablestring = "Horse"
variablestring + = "and carriage"
//Variablestring value is now "horse and carriage"
     
le T constantstring = "Highlander"
constantstring + = "and another Highlander"
//Compilation error-The value of a constant string cannot be changed

Note:

This implementation differs from the objective-c/Cocoa string variability, which is done by selecting one of the two classes to which the instance belongs (nsstring or nsmutablestring To declare whether the string is variable.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.