Swift Study notes-3. Strings and characters

Source: Internet
Author: User

Initializing an empty string

Create an empty string, you can assign an empty string literal to a variable, or you can initialize a new string instance: var emptystring = ""
var anotheremptystring = String ()
Two strings are empty, and equality can be determined by checking the IsEmpty property of its Boolean type whether the string is empty: if Emptystring.isempty {
println ("Nothing")
} string VariabilityAssign a specific string to a variable to modify it, or assign it to a constant to ensure it is not modified: var variablestring = "Horse"
Variablestring + = "and carriage"

Let constantstring = "Highlander"
Constantstring + = "and another Highlander"//Compile Error Note: In Swift, whether a string can be modified is determined only by defining a variable or constant, which enables the unification of multiple types of variability operations. string is a value typeThe string type is a value type, a string that is copied when it is passed in a constant, variable assignment operation, or in a function or method. In any case, a new copy of the existing string is created and the new copy is passed or assigned. using charactersThe string type represents a collection of Character type values for a particular sequence, and you can use for in to iterate through each character in the string. For character in "dog!" {
println (character)
In addition, you can create a separate character constant or variable by indicating a Character type and copying it by character literal: let Yensign:character = "¥" calculating the number of charactersBy using the global countelements function of the bar, you can get the number of characters in a string let Unusualmenagerie = "Koala, Snail, Penguin, dromedary"
println ("Unusualmenagerie has \ (countelements (Unusualmenagerie)) characters)" Connection StringYou can connect two strings by "+" let string1 = "Hello"
Let string2 = "there"

var welcome = string1 + string2 You can use "+ =" to append a string to a string variable var instruction = "Look over"
Instruction + = string2//"Look over There" can append a Character variable to a string variable by using the string's Append method Exclamationmark:cha Racter = "!"
Welcome.append (Exclamationmark)//"Hello there!" NOTE: You cannot append a String or Character after a Character variable, because Character can contain only one character string InterpolationString interpolation is a way to construct a new string that can contain constants, variables, literals, and expressions. Each entry of the inserted string literal is wrapped in "\ ()" In Let multiplier = 3
Let message = "\ (multiplier) times 2.5 is \ (Double (multiplier) * 2.5)" Note: An expression written in parentheses in an interpolated string cannot contain a non-escaped double quotation mark (") and a backslash (\). and cannot contain carriage returns or line feeds. Comparing StringsThree ways to compare strings: strings are equal, prefixes are equal, suffixes are equal string EqualityIf two strings contain exactly the same characters in the same order, the two strings are considered equal let quotation = "We ' re a lot alike, you and I"
Let samequotation = "We ' re a lot alike, you and I"

If quotation = = Samequotation {
println ("These, strings is considered equal")
} The prefix is equalCheck whether a string has a specific prefix or suffix by calling the Hasprefix/hassuffix method of string. Let Remeoandjuliet = [
"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"
]

var act1scenecount = 0
For scene in Remeoandjuliet {
If Scene.hasprefix ("Act 1") {
++act1scenecount;
}
}
println ("There is \ (Act1scenecount) scenes in Act 1")
var mansioncount = 0
var cellcount = 0
For scene in Remeoandjuliet {
If Scene.hassuffix ("Capulet ' s Mansion") {
++mansioncount
} else if Scene.hassuffix ("Friar Lawrence ' s cell") {
++cellcount
}
}

println ("\ (mansioncount) mansion scenes; \ (cellcount) cell scenes ") uppercase and lowercase strings can be accessed through the string's uppercasestring and LowerCaseString properties to access the uppercase \ Lowercase version of the string let normal =" Could you h ELP me, please? "
Let shouty = normal.uppercasestring
Let whispered = normal.lowercasestring

Swift Study notes-3. 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.