01-string definitions and basic operations
//strings string is an ordered collection of character character//"Literal of string"Varstring="Hello"//two ways to create an empty stringvar null string =""var empty string 2=String ()ifAn empty string. IsEmpty {println ("This is an empty string")} Else{println ("This string has a value")}//Modifying a stringstring="Hello World"//Stitching Stringsstring+="!!!"//iterates through the characters in string character forCharacterinch string{println (character)}let one character: Character="a"//Note: You cannot assign string strings to characters character//UnicodeLet the shining love ="\u{1f497}"//computes the number of character character in a stringCountelements (string)
02-Comparison of strings
//Comparison of Strings Letstring="Hello world!"Let string2="Hello world!"if(string==string2) {println ("the two strings are equal")} Else{println ("these two strings are not equal")}//prefixes and suffixes of stringsLet area list = [ "Baoshan District, Shanghai city", "Xuhui District, Shanghai", "Putuo District, Shanghai", "Chongming District, Shanghai city", "Dalian Zhongshan District", "Dalian Lvshun District", "Dalian Island", "Nanshan District, Shenzhen", "Shenzhen Futian District", "Zhoushan Putuo", "Tashikuergantajikezizhi County", "Seoul City Jiangnan District", "Hegang South Mt. District", "Zhongshan District, Taipei", "Muleihasakezizhi County", "Wuhu Nanling County", "Bortala Mongolian Autonomous Prefecture", "Bayingolin Mongolian Autonomous Prefecture", "Yichun Youhao District", "Minato-ku, Tokyo", "Shuangyashan Baoshan", "Fuyuan County Heixiazi Island", "Mariala Islands", "Zhoushan Qundao", "Thousand Island Islands", "Guam", "Nanning Jiangnan District", "Hotan County Xinjiang Uygur Autonomous Region"number of]var districts=0number of Var counties=0var prefecture number=0number of Var islands=0var Baoshan number=0var belongs to Shanghai=0var belongs to Dalian=0var zone list=[String] () var county list=list of [String] () var prefectures=[String] () var island list=[String] () var Baoshan list=[String] () var belongs to Shanghai list=[String] () var belongs to Dalian List=[String] () forAreainchRegion List {//determine if the suffix of the region contains a "zone" if(Region. Hassuffix ("Area")) { //number of self-update zones++Number of districts//Insert region at the end of a list of extentsList of zones. Append (region)}if(Region. Hassuffix ("County")) { ++County number County list. Append (region)}if(Region. Hassuffix ("Autonomous Prefecture")) { ++list of autonomous prefectures. Append (region)}if(Region. Hassuffix ("Island")) { ++List of Island Islands. Append (region)}if(Region. Hassuffix ("Baoshan")) { ++Baoshan number Baoshan list. Append (region)}//determine if the prefix of the region contains "Shanghai" if(Region. Hasprefix ("Shanghai")) { ++Shanghai is a list of Shanghai . Append (region)}if(Region. Hasprefix ("Dalian")) { ++Dalian belongs to Dalian List. Append (region)}} println ("Total (number of districts) area:") forAreainchList of districts {println (zone)}
03-String Unicode encoding
Let greeting ="Hello hiこんにちは????? \u{1f496}"println ("\ (Greetings)") println ("\nutf-8 Code:") forCode unitinchgreetings. UTF8 {print ("\ (code unit)")}println ("\nutf-16 Code:") forCode unitinchgreetings. utf16 {print ("\ (code unit)")}println ("\nutf-32 Code:") forUniversal Code Scalarinchgreetings. unicodescalars {print ("\ (Universal code scalar. Value)")}
Swift Strings and characters