Strings and Characters¶A string is a series of characters, such as Show
Swift’s Despite this simplicity of syntax, Swift’s Note Swift’s For more information about using String Literals¶You can include predefined Use a string literal as an initial value for a constant or variable:
Note that Swift infers a type of Multiline String Literals¶If you need a string that spans several lines, use a multiline string literal—a sequence of characters surrounded by three double quotation marks:
A multiline string literal includes all of the lines between its opening and
closing quotation marks. The string begins on the first line after the opening quotation marks (
When
your source code includes a line break inside of a multiline string literal, that line break also appears in the string’s value. If you want to use line breaks to make your source code easier to read, but you don’t want the line breaks to be part of the string’s value, write a backslash (
To make a multiline string literal that begins or ends with a line feed, write a blank line as the first or last line. For example:
A multiline string can be indented to match the surrounding code. The whitespace before the closing quotation marks ( In the example above, even though the entire multiline string literal is indented, the first and last lines in the string don’t begin with any whitespace. The middle line has more indentation than the closing quotation marks, so it starts with that extra four-space indentation. Special Characters in String Literals¶String literals can include the following special characters:
The code below shows four examples of these special characters. The
Because multiline string literals use three double quotation marks instead of just one, you can include a double quotation mark (
Extended String Delimiters¶You can place a string literal within extended delimiters to include special characters in a string without invoking their effect. You place your
string within quotation marks ( If you need the special effects of a character in a string literal, match the number of number signs within the string following the escape character ( String literals created using extended delimiters can also be multiline string literals. You can use extended delimiters to include the text
Initializing an Empty String¶To create an empty
Find out whether a
String Mutability¶You indicate whether a particular
Note This approach
is different from string mutation in Objective-C and Cocoa, where you choose between two classes ( Strings Are Value Types¶Swift’s Swift’s copy-by-default Behind the scenes, Swift’s compiler optimizes string usage so that actual copying takes place only when absolutely necessary. This means you always get great performance when working with strings as value types. Working with Characters¶You can access the individual
The Alternatively, you can create a stand-alone
Concatenating Strings and Characters¶
You can also append a
You can append a
Note You can’t append a If you’re using multiline string literals to build up the lines of a longer string, you want every line in the string to end with a line break, including the last line. For example:
In the code above, concatenating String Interpolation¶String interpolation is a way to construct a new
In the
example above, the value of The value of You can use extended string delimiters to create strings containing characters that would otherwise be treated as a string interpolation. For example:
To use string interpolation inside a string that uses extended delimiters, match the number of number signs after the backslash to the number of number signs at the beginning and end of the string. For example:
Note The expressions you write inside parentheses within an interpolated string can’t contain an unescaped backslash ( Unicode¶Unicode is an international standard for encoding, representing, and processing text in different writing systems. It enables you to represent almost any character from any language in a standardized form, and to read and write those characters to and
from an external source such as a text file or web page. Swift’s Unicode Scalar Values¶Behind the scenes, Swift’s native Note that not all 21-bit Unicode scalar values are assigned to a character—some scalars are reserved for future assignment or for use in UTF-16 encoding. Scalar values that have been assigned to a character typically also have a name, such as Extended Grapheme Clusters¶Every instance of Swift’s Here’s an example. The letter In both
cases, the letter
Extended grapheme clusters are a flexible way to represent many complex script characters as a single
Extended grapheme clusters enable scalars for enclosing marks (such as
Unicode scalars for regional indicator symbols can be combined in pairs to make a single
Counting Characters¶To retrieve a count of the
Note that Swift’s use of extended grapheme clusters for For example, if you initialize a new string with the four-character word
Note Extended grapheme clusters can be composed of multiple Unicode
scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift don’t each take up the same amount of memory within a string’s representation. As a result, the number of characters in a string can’t be calculated without iterating through the string to determine its extended grapheme cluster boundaries. If you are working with particularly long string values, be aware that the
The count of the characters returned by the Accessing and Modifying a String¶You access and modify a string through its methods and properties, or by using subscript syntax. String Indices¶Each
As mentioned above, different characters can require different amounts of memory to store, so in order to determine which Use the You access the indices before and after a given index using the You can use subscript syntax to access the
Attempting to access an index outside of a string’s range or a
Use the
Note You can use the Inserting and Removing¶To insert a single character into a string at a specified index, use the
To remove a single character from a string at a specified index, use the
Note You can use the Substrings¶When you get a substring from a string—for example, using a subscript or a method like
Like strings, each substring has a region of memory where the characters that make up the substring are stored. The difference between strings and substrings is that, as a performance optimization, a substring can reuse part of the memory that’s used to store the original string, or part of the memory that’s used to store another substring. (Strings have a similar optimization, but if two strings share memory, they’re equal.) This performance optimization means you don’t have to pay the performance cost of copying memory until you modify either the string or substring. As mentioned above, substrings aren’t suitable for long-term storage—because they reuse the storage of the original string, the entire original string must be kept in memory as long as any of its substrings are being used. In the example above, Note Both Comparing Strings¶Swift provides three ways to compare textual values: string and character equality, prefix equality, and suffix equality. String and Character Equality¶String and character equality is checked with the “equal to” operator (
Two For example,
Conversely,
Note String and character comparisons in Swift aren’t locale-sensitive. Prefix and Suffix Equality¶To check whether a string has a particular string prefix or suffix, call the string’s The examples below consider an array of strings representing the scene locations from the first two acts of Shakespeare’s Romeo and Juliet:
You can use the
Similarly, use the
Note The Unicode Representations of Strings¶When a Unicode string is written to a text file or some other storage, the Unicode scalars in that string are encoded in one of several Unicode-defined encoding forms. Each form encodes the string in small chunks known as code units. These include the UTF-8 encoding form (which encodes a string as 8-bit code units), the UTF-16 encoding form (which encodes a string as 16-bit code units), and the UTF-32 encoding form (which encodes a string as 32-bit code units). Swift provides several different ways to access Unicode representations of strings. You can iterate over the string with a Alternatively, access a
Each example below shows a different representation of the following string, which is made up of the characters
UTF-8 Representation¶You can access a UTF-8 representation of a
In the example above, the first three decimal UTF-16 Representation¶You can access a UTF-16 representation of a
Again,
the first three The fourth The fifth and sixth Unicode Scalar Representation¶You
can access a Unicode scalar representation of a Each
The The fourth The As an alternative to querying their
Which method would you use to determine whether a certain substring is a suffix of a string?prototype. endsWith() The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.
Which method would you use to determine whether a certain substring is present in a string quizlet?Which method would you use to determine whether a certain substring is present in a string? The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.
Which of the following string methods can be used to determine if a string contains only \n?Which of the following string methods can be used to determine if a string contains only '\n' characters? The right side of the '*' must be an integer.
How do you find a substring in a string?You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.
|