Swift Learning notes-strings and characters (Strings and characters)-Compare strings (comparing Strings)

Source: Internet
Author: User

Swift provides three ways to compare text values: String characters typeface, prefixes equal, and suffixes equal.

string/character typeface (string and Character equality)

The string/character can be used as the Equals operator (==) and not equal to the operator (!=), described in detail in the comparison operator:

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 two strings are considered equal")
// printout "These two strings are considered equal"

If two strings (or two characters) of extensible glyph clusters are standard equal, they are considered equal. In this case, even if the extensible Glyph cluster is composed of different Unicode scalars, they are considered to be of equal value as long as they have the same linguistic meaning and appearance.

For example,LATIN SMALL LETTER E WITH ACUTE()U+00E9is the standard equal toLATIN SMALL LETTER E(U+0065) followed byCOMBINING ACUTE ACCENT(U+0301). Both of these clouds sets are valid ways of representing charactersé, so they are considered to be of equal standard:

// "Voulez-vous un café?" Using LATIN SMALL LETTER E WITH ACUTE
Let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"

// "Voulez-vous un café?" Using LATIN SMALL LETTER E and COMBINING ACUTE ACCENT
Let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?"

If eAcuteQuestion == combinedEAcuteQuestion {
     Print("These two strings are considered equal")
// printout "These two strings are considered equal"

Conversely, EnglishLATIN CAPITAL LETTER A(U+0041orA) is not equal toCYRILLIC CAPITAL LETTER A(, or) in RussianU+0410A. Two characters look the same, but there are different language meanings:

Let latinCapitalLetterA: Character = "\u{41}"

Let cyrillicCapitalLetterA: Character = "\u{0410}"

If latinCapitalLetterA != cyrillicCapitalLetterA {
     Print("These two characters are not equivalent")
// print "These two characters are not equivalent"

Note: In Swift, strings and characters are not region-sensitive.

prefix/suffix equal (Prefix and Suffix Equality)

By invoking the/method of the stringhasPrefix(_:)hasSuffix(_:)to check whether a string has a specific prefix/suffix, two methods receive aStringparameter of type and return a Boolean value.

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 room in 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 callhasPrefix(_:)methods 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 ") {
Print("There are \(act1SceneCount) scenes in Act 1")
// printout "There are 5 scenes in Act 1"

Similarly, you can usehasSuffix(_:)methods 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") {
     } else if scene.hasSuffix("Friar Lawrence‘s cell") {
Print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
// printout "6 mansion scenes; 2 cell scenes" 

Note: The and methods are each character-by-character comparison of thehasPrefix(_:)hasSuffix(_:)clouds set of its extensible characters in each string, described in detail in string/Word typeface, and so on.

Swift Learning notes-strings and characters (Strings and characters)-Compare strings (comparing Strings)

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.