iOS Swift Learning Diary 4-strings and characters

Source: Internet
Author: User
Tags scalar

String is a collection of character (character)-type values such as "Hello, World", "Thief King", represented by a string type.


Swift's string and character types provide a fast, Unicode-compatible way to handle textual information in code. The syntax for creating and manipulating strings is similar to string manipulation in C, lightweight and easy to read. The string connection operation requires simply connecting two strings with the + sign. As with other values in Swift, you can change the value of a string, depending on whether it is defined as a constant or a variable.


Although syntax is simple, the string type is a fast, modern string implementation. Each string consists of independently encoded Unicode characters and provides support for accessing these characters in different Unicode representations (representations).


Swift can do string interpolation in constants, variables, literals, and expressions, making it easy to create custom strings for presentation, storage, and printing.


Attention:


Swift's string type is seamlessly bridged with the Foundation NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch. All NSString APIs can invoke values of any string type that you create. In addition to this, you can also use the string attribute described in this chapter. You can also use string-type values as an alternative to any API that requires incoming NSString instances as parameters.


For more information on using string in Foundation and Cocoa, see Using Swift with Cocoa and objective-c.


string literal (string literals)
You can include a predefined string value in your code as a string literal. String literals are wrapped in double quotation marks ("") with a fixed-order text character set.


String literals can be used to provide initial values for constants and variables.


Let somestring = "Some string literal value"
Attention:


The somestring variable is initialized by the string literal, and Swift infers that the variable is of type string.
String literals can contain the following special characters:


escape character, \ \ (backslash), \ t (horizontal tab), \ n (line break), \ r (carriage return), \ "(double quotation mark), \ ' (single quote).
A single-byte Unicode scalar, written as \xnn, where nn is a two-bit hexadecimal number.
A double-byte Unicode scalar, written as \unnnn, where nnnn is a four-bit hexadecimal number.
A four-byte Unicode scalar, written as \unnnnnnnn, where nnnnnnnn is a eight-bit hexadecimal number.
The following code is an example of the use of various special characters. The Wisewords constant contains two transfer special characters (double brackets); dollarsign, Blackheart, and Sparklingheart constants demonstrate three different formats of Unicode scalars:


Let wisewords = "\" I'm the man who's going to be the king of the Pirates. "-Luffy"
"I am the man to be the king of the thieves"-Luffy
Let dollarsign = "\x24"//$, Unicode scalar u+0024
Let Blackheart = "\u2665"//?, Unicode scalar u+2665
Let Sparklingheart = "\u0001f496"//??, Unicode scalar u+1f496


Initialize empty string (Initializing an empty string)
To construct a long string, you can create an empty string as the initial value. You can assign an empty string literal to a variable, or you can initialize a new string instance:


var emptystring = ""//empty string literal
var anotheremptystring = string ()//Initialize string instance
The two strings are both empty and equivalent.
You can determine whether the string is empty by checking its Boolean IsEmpty property:


If Emptystring.isempty {
println ("Nothing")
}
Print output: "Nothing at all"


String variability (String mutability)
You can modify a specific string by assigning it to a variable, or assign it to a constant to ensure that it is not modified:


var variablestring = "Horse"
Variablestring + = "and carriage"
Variablestring is now "Horse and carriage"
Let constantstring = "Highlander"
Constantstring + = "and another Highlander"
This will report a compilation error (compile-time error)-Constants cannot be modified.
Attention:


In Objective-c and Cocoa, you specify whether the string can be modified by selecting two different classes (NSString and nsmutablestring), whether the string in Swift can be modified only by defining a variable or constant, It realizes the unification of various types of variability operations.


String is a value type (Strings is value Types)
Swift's string type is a value type. If you create a new string, the value is copied when it is passed in a constant, variable assignment operation, or in a function/method. In any case, a new copy of the existing string value is created and the new copy is passed or assigned. Value types are described in structs and enumerations as value types.


Attention:


Unlike nsstring in Cocoa, when you create a NSString instance in Cocoa and pass it to a function/method, or assign a value to a variable, you pass or assign a reference to the NSString instance unless you specifically require a value copy, Otherwise, the string does not generate a new copy for the assignment operation.
The Swift default string copy ensures that the value of the string is passed in the function/method. It's obvious that wherever that value comes from, it's all on your own. You can rest assured that the strings you pass are not changed by themselves.


At actual compile time, the Swift compiler optimizes the use of strings so that actual replication occurs only when absolutely necessary, which means that you can achieve very high performance when you use strings as value types.




Using characters (working with characters)
Swift's String type represents a collection of character (character) type values for a particular sequence. Each character value represents a Unicode character. You can use the for-in loop to iterate through each character in a string:


For character in "Dog!??" {
println (character)
}
D
O
G
// !
// ??
The for-in loop is described in detail in the for Loops.


In addition, you can create an independent character constant or variable by marking a character type annotation and assigning it by character literal:


Let Yensign:character = "¥"


Calculate the number of characters (counting characters)
You can get the number of characters in the string by calling the global countelements function and passing the string as an argument.


Let Unusualmenagerie = "Koala??, Snail??, Penguin??, dromedary??"
println ("Unusualmenagerie has \ (countelements (Unusualmenagerie)) characters")
Print output: "Unusualmenagerie has characters"
Attention:


Different Unicode characters and different representations of the same Unicode characters may require a different amount of memory space to store. Therefore, the characters in Swift do not necessarily occupy the same memory space in a string. So the length of the string has to be computed by iterating through the length of each character in the string. If you are working with a long string, be aware that the countelements function must traverse the characters in the string to accurately calculate the length of the string.


It is also important to note that the number of characters returned by Countelements is not always the same as the length property of NSString that contain the same character. The length property of NSString is based on the number of 16-bit code units represented by the UTF-16, rather than Unicode characters. To solve this problem, NSString's Length property becomes utf16count when accessed by Swift's string.


Connection strings and characters (concatenating Strings and characters)
The values of strings and characters can be added together with the addition operator (+) and create a new string value:


Let string1 = "Hello"
Let string2 = "there"
Let Character1:character = "!"
Let Character2:character = "?"


Let Stringpluscharacter = string1 + character1//equals "hello!"
Let stringplusstring = string1 + string2//equals "Hello there"
Let characterplusstring = character1 + string1//equals "!hello"
Let Characterpluscharacter = Character1 + character2//equals "!?"
You can also add a string or character to an already existing string variable by using the addition assignment operator (+ =):


var instruction = "Look over"
Instruction + = string2
Instruction now equals "look over there."


var welcome = "Good Morning"
Welcome + = Character1
Welcome now equals "good morning!."
Attention:


You cannot add a string or character to a character variable that already exists because the character variable can contain only one character.


string interpolation (string interpolation)
String interpolation is a way to construct a new string that can contain constants, variables, literals, and expressions. Each entry of the string literal that you insert is wrapped in parentheses prefixed with a backslash:


Let multiplier = 3
Let message = "\ (multiplier) multiplied by 2.5 is \ (Double (multiplier) * 2.5)"
The message is "3 times 2.5 is 7.5"
In the example above, multiplier as \ (multiplier) is inserted into a string literal. When you create a string to perform an interpolation calculation, this placeholder is replaced with the multiplier actual value.


The value of multiplier also acts as part of the subsequent expression in the string. The expression evaluates the value of double (multiplier) * 2.5 and inserts the result (7.5) into the string. In this example, the expression is written as \ (Double (multiplier) * 2.5) and is included in the string literal.


Attention:


Expressions written in parentheses in your interpolated string cannot contain non-escaped double quotation marks (") and backslashes (\), and cannot contain carriage returns or line feeds.


Compare strings (comparing Strings)
Swift provides three ways to compare the values of strings: strings are equal, prefixes are equal, and suffixes are equal.




strings equal (string equality)
If two strings contain exactly the same characters in the same order, the strings are considered equal:


Let quotation = "We are the same drops."
Let samequotation = "We are the same drops."
If quotation = = Samequotation {
println ("These two strings are considered to be the same")
}
Print output: "These two strings are considered the same"


prefix/suffix equal (Prefix and Suffix Equality)
Checks whether a string has a specific prefix/suffix by calling the Hasprefix/hassuffix method of the string. All two methods need to pass in a string as a parameter and outgoing a Boolean value. Two methods perform a character-by-byte comparison between the base string and the prefix/suffix string.


The following example shows the position of the first two scenes of the Shakespeare play Romeo and Juliet in a string array:


Let Romeoandjuliet = [
"Act 1 Scene 1:verona, A public Place",
"Act 1 Scene 2:capulet ' s Mansion",
ACT 1 Scene 3: A Capulet ' s Mansion ",
"Act 1 Scene 4:a Street outside Capulet ' s mansion",
"Act 1 Scene 5:the great Hall in Capulet ' s mansion",
"Act 2 Scene 1:outside Capulet ' s Mansion",
"Act 2 Scene 2:capulet ' s Orchard",
"Act 2 Scene 3:outside friar Lawrence ' s Cell",
"Act 2 Scene 4:a Street in Verona",
"Act 2 Scene 5:capulet ' s Mansion",
"Act 2 Scene 6:friar Lawrence ' s Cell"
]
You can use the Hasprefix method to calculate the number of scenes in the first scene of a play:


var act1scenecount = 0
For scene in Romeoandjuliet {
If Scene.hasprefix ("Act 1") {
++act1scenecount
}
}
println ("There is \ (Act1scenecount) scenes in Act 1")
Print output: "There is 5 scenes in Act 1"
Similarly, you can use the Hassuffix method to calculate the number of scenes that occur in different places:


"var mansioncount = 0
var cellcount = 0
For scene in Romeoandjuliet {
If Scene.hassuffix ("Capulet ' s Mansion") {
++mansioncount
} else if Scene.hassuffix ("Friar Lawrence ' s cell") {
++cellcount
}
}
println ("\ (mansioncount) mansion scenes; \ (cellcount) cell scenes ")
Print output: "6 mansion scenes; 2 cell scenes "


Uppercase and lowercase strings (uppercase and lowercase Strings)
You can access the uppercase/lowercase version of the string by using the Uppercasestring and LowerCaseString properties of the string.


Let normal = "Could to help me, please?"
Let shouty = normal.uppercasestring
shouty value for "COULD you help ME, please?"
Let whispered = normal.lowercasestring
Whispered value for "Could you help me, please?"


Unicode
Unicode is an international standard for encoding and representation of text. It enables you to represent almost all characters from any language in a standard format and to read and write to characters in external resources such as text files or Web pages.


Swift's string and character types are fully compatible with the Unicode standard, and it supports a range of different Unicode encodings as described below.




Unicode terminology (Unicode terminology)
Each character in Unicode can be interpreted as one or more Unicode scalars. The Unicode scalar of a character is a unique 21-bit number (and name), such as u+0061 for lowercase Latin alphabet A ("a"), and u+1f425 for the chicken expression ("??")


When a Unicode string is written into a text file or other storage structure, these Unicode scalars are encoded in one of the centralized formats defined by Unicode. It includes UTF-8 (encoded in 8-bit code units) and UTF-16 (encoded in 16-bit code units).




Unicode representation of a string (Unicode representations of Strings)
Swift provides several different ways to access the Unicode representation of a string.


You can use For-in to traverse a string to access each character value as a Unicode character. The procedure is described in the use of characters.


In addition, the value of the string can be accessed in three other Unicode-compatible ways:


UTF-8 Code Unit Collection (accessed using the UTF8 property of the string)
UTF-16 Code Unit Collection (accessed using the Utf16 property of the string)
21-bit Unicode scalar value collection (accessed using the Unicodescalars property of the string)
Below by d ' o ' ' g '! Each character in a string consisting of the DOG Face,unicode scalar is u+1f436 represents a different representation:


Let dogstring = "Dog!??"


UTF-8
You can access its UTF-8 representation by traversing the UTF8 property of the string. It is an attribute of type Utf8view, Utf8view is a collection of unsigned 8-bit (UInt8) values, and each UInt8 value is a UTF-8 representation of a character:


For CodeUnit in Dogstring.utf8 {
Print ("\ (codeUnit)")
}
Print ("\ n")
68 111 103 33 240 159 144 182
In the above example, the first four 10 binary code unit values (68, 111, 103, 33) represent the characters d o G and!, their UTF-8 representation is the same as the ASCII representation. The following four code unit values (240, 159, 144, 182) are the 4-byte UTF-8 representation of the dog face.




UTF-16
You can access its UTF-16 representation by traversing the Utf16 property of the string. It is an attribute of type Utf16view, Utf16view is a collection of unsigned 16-bit (UInt16) values, and each UInt16 is a UTF-16 representation of a character:


For CodeUnit in Dogstring.utf16 {
Print ("\ (codeUnit)")
}
Print ("\ n")
68 111 103 33 55357 56374
Similarly, the first four code unit values (68, 111, 103, 33) represent the characters d o G and!, their UTF-16 code unit and UTF-8 are exactly the same.


The five and Sixth code unit values (55357 and 56374) are UTF-16 representations of the dog face character. The first value is u+d83d (decimal value is 55357) and the second value is u+dc36 (decimal value is 56374).




Unicode scalar (Unicode scalars)
You can access its Unicode scalar representation by traversing the Unicodescalars property of the string. It is an attribute of type Unicodescalarview, and Unicodescalarview is a collection of unicodescalar. The Unicodescalar is a 21-bit Unicode code point.


Each unicodescalar has a value attribute and can return the corresponding 21-bit value, expressed in UInt32.


For scalar in Dogstring.unicodescalars {
Print ("\ (scalar.value)")
}
Print ("\ n")
68 111 103) 33 128054
Similarly, the first four code unit values (68, 111, 103, 33) represent the characters d o G and!. The fifth digit value, 128054, is a decimal representation of a hexadecimal 1f436. It is equivalent to the Unicode scalar u+1f436 of the dog face.


As an alternative to querying character-valued properties, each Unicodescalar value can also be used to construct a new string value, such as in string interpolation:


For scalar in Dogstring.unicodescalars {
println ("\ (scalar)")
}
D
O
G
// !
// ??

iOS Swift Learning Diary 4-strings and characters

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.