Swift Learning notes-tutorials to learn two strings and characters (Strings and characters)

Source: Internet
Author: User
Tags scalar

Follow the Swift tutorial to record what you think is important. --New Wave

2.1string literalString Literals

string literals are represented by double quotation marks ( "" ) wrapped with a fixed-order text character set.

Let somestring = "Some string literal value"

2.2initializing an empty stringInitializing an Empty String

var emptystring = ""//empty string literal

var anotheremptystring = String ()//construction method

The two strings are both empty and equivalent.

2.3string VariabilityString mutability

(with Let a string assigned to a constant can no longer be modified.

2.4string is a value typeStrings is Value Types

Because a string is a value type, the assignment, copy, and pass process does not affect the original string, it creates a new copy and passes or assigns the new copy.

2.5using charactersworking with characters

by marking a Character type and the character literal to assign the value,

Let Exclamationmark:character = "!"

Let catcharacters: [Character] = ["C", "a", "T", "!", "??"]

Let catstring = String (catcharacters)

Print (catstring)

Prints "Cat!??"

2.6connection strings and charactersconcatenating Strings and characters

Let string1 = "Hello"

Let string2 = "there"

var welcome = string1 + string2

Welcome now equals "Hello there."

var instruction = "Look over"

Instruction + = string2

Instruction now equals "look over there."

Let Exclamationmark:character = "!

Welcome.append (Exclamationmark)

Welcome now equals "Hello there!."

2.7string Interpolation(String interpolation)

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

Let multiplier = 3

Let message = "\ (multiplier) times 2.5 is \ (Double (multiplier) * 2.5)"

Message is ' 3 times 2.5 is 7.5

2.8 UnicodeUnicodeScalar (Unicode scalars)

Swiftof theStringtype is based onUnicodescalarestablished. Unicodea scalar is the only one that corresponds to a character or modifier. +bit numbers, such asu+0061the Latin alphabet that represents lowercase(LATIN SMALL letter A) ("a"),u+1f425expression of a chick(front-facing BABY chick) ("?").

special characters for string literals(special characters in String literals)

String literals can contain the following special characters:

• Escape character (null character), \ \ (backslash), \ t (horizontal tab), \ n (newline character), \ r (carriage return), \ "(double quotation mark), \ ' (single quotation mark).

Unicode scalar, written as \u{n} (U is lowercase), where n is any one to eight hexadecimal digits and the available Unicode bit code.

Scalable Glyph Clusters(Extended grapheme Clusters)

Let Eacute:character = "\u{e9}"//é

Let Combinedeacute:character = "\u{65}\u{301}"//E-followed by?

Eacute isé, Combinedeacute is E?

2.9calculating the number of charactersCounting characters

If you want to get a string in the Character the number of strings that can be used characters property of Count Properties:

Note: The extensible character clouds set can form one or more Unicode scalars.

The length property of the NSString is the 16-bit code unit number represented by the UTF-16.

var word = "Cafe"

Print ("The number of characters in \ (word) is \ (Word.characters.count)")

Prints "The number of characters in Cafe is 4"

Word + = "\u{301}"//combining ACUTE ACCENT, u+0301

Print ("The number of characters in \ (word) is \ (Word.characters.count)")

Prints "The number of characters in cafe? is 4 "

2.10Accessing and modifying stringsAccessing and modifying a StringString IndexString Indices

each one String value has an associated index type, String.index that corresponds to each of the characters in the string Character the location.

Let greeting = "Guten tag!"

Greeting[greeting.startindex]

G

Greeting[greeting.endindex.predecessor ()]

// !

Greeting[greeting.startindex.successor ()]

U

Let index = greeting.startIndex.advancedBy (7)

Greeting[index]

A

Greeting[greeting.endindex]//Error

Greeting.endIndex.successor ()//Error

For index in Greeting.characters.indices {

Print ("\ (Greeting[index])", Terminator: "")

}

Prints "G U t e n T a G!"

inserting and deletingInserting and removing

Insert (_:atindex:), Insertcontentsof (_:at:), Removeatindex (_:)

var welcome = "Hello"

Welcome.insert ("!", AtIndex:welcome.endIndex)

Welcome now equals "Hello!"

Welcome.insertcontentsof ("there". Characters, At:welcome.endIndex.predecessor ())

Welcome now equals "Hello there!"

Welcome.removeatindex (Welcome.endIndex.predecessor ())

Welcome now equals "Hello there"

To remove a substring at a specified range, use the RemoveRange (_:) Method:

Let range = Welcome.endIndex.advancedBy (-6): <welcome.endindex

Welcome.removerange (Range)

Welcome now equals "Hello"

2.11Comparing Strings(comparing Strings)string/Word typeface, etc.(String and Character equality)

equals ( == ) and not equal to ( != )

Let quotation = "We ' re a lot alike, you and I."

Let samequotation = "We ' re a lot alike, you and I."

If quotation = = Samequotation {

Print ("These strings is considered equal")

}

Print output "These strings is considered equal"

"Voulez-vous un café?" using LATIN SMALL letter E with ACUTE

Let eacutequestion = "voulez-vous un caf\u{e9}?"

"Voulez-vous un cafe?" using LATIN SMALL letter E and combining ACUTE ACCENT

Let combinedeacutequestion = "voulez-vous un caf\u{65}\u{301}?"

if eacutequestion = = combinedeacutequestion {

Print ("These strings is considered equal")

}

prefix/suffix equal(Prefix and Suffix equality)

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 call Hasprefix (_:) method to calculate the number of scenes in the first act of a play:

var act1scenecount = 0

For scene in Romeoandjuliet {

If Scene.hasprefix ("Act 1") {

Act1scenecount + = 1

}

}

Print ("There is \ (Act1scenecount) scenes in Act 1")

Prints "There is 5 scenes in Act 1"

Similarly, you can use 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 + = 1

} else if Scene.hassuffix ("Friar Lawrence ' s cell") {

Cellcount + = 1

}

}

Print ("\ (mansioncount) mansion scenes; \ (cellcount) cell scenes ")

Prints "6 mansion scenes; 2 cell scenes "

of the stringUnicodeRepresentation (Unicode representations of Strings)

When a Unicode string is written into a text file or other storage, the Unicode scalar in the string is encoded in several encoding formats defined by Unicode. The small block encoding in each string is called a code unit. The encoding format includes the UTF-8 encoding format (a code unit with an encoded string of 8 bits), a UTF-16 encoding format (a code unit with an encoded string of 16 bits), and a UTF-32 encoding format (a code unit with an encoded string of 32 bits).

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, which is the UTF-32 encoding format of the string (accessed using the Unicodescalars property of the string)

Let dogstring = "Dog???" (u+1f436)

UTF-8 Representation

For CodeUnit in Dogstring.utf8 {

Print ("\ (CodeUnit)", Terminator: "")

}

Print ("")

68 111 103 226 128 188 240 159 144 182

UTF-16 Representation

For CodeUnit in Dogstring.utf16 {

Print ("\ (CodeUnit)", Terminator: "")

}

Print ("")

68 111 103 8252 55357 56374

Unicode Scalar Representation

For scalar in Dogstring.unicodescalars {

Print ("\ (Scalar.value)", Terminator: "")

}

Print ("")

68 111 103) 8252 128054

For scalar in Dogstring.unicodescalars {

Print ("\ (scalar)")

}

D//O//g//? // ??

Swift Learning notes-tutorials to learn two strings and characters (Strings and characters)

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.