Swift-2.3瀛楃涓插拰瀛楃

鏈〉鍖呭惈鍐呭锛�/p>

  • 瀛楃涓插瓧闈㈤噺
  • 鍒濆鍖栫┖瀛楃涓�/a>
  • 瀛楃涓插彲鍙樻�
  • 瀛楃涓叉槸鍊肩被鍨�/a>
  • 浣跨敤瀛楃
  • 杩炴帴瀛楃涓插拰瀛楃
  • 瀛楃涓叉彃鍊�/a>
  • Unicode
  • 璁$畻瀛楃鏁伴噺
  • 璁块棶鍜屼慨鏀瑰瓧绗︿覆
  • 姣旇緝瀛楃涓�/a>
  • 瀛楃涓茬殑 Unicode 琛ㄧず褰㈠紡

String鏄緥濡�hello, world"锛�albatross"杩欐牱鐨勬湁搴忕殑Character锛堝瓧绗︼級绫诲瀷鐨勫�鐨勯泦鍚堛�閫氳繃String绫诲瀷鏉ヨ〃绀恒� 涓�釜String鐨勫唴瀹瑰彲浠ョ敤鍙橀噺鐨勬柟寮忚鍙栵紝瀹冨寘鎷竴涓�code>Character鍊肩殑闆嗗悎銆�br />鍒涘缓鍜屾搷浣滃瓧绗︿覆鐨勮娉曚笌 C 璇█涓瓧绗︿覆鎿嶄綔鐩镐技锛岃交閲忓苟涓旀槗璇汇� 瀛楃涓茶繛鎺ユ搷浣滃彧闇�绠�崟鍦伴�杩�code>+绗﹀彿灏嗕袱涓瓧绗︿覆鐩歌繛鍗冲彲銆備笌 Swift 涓叾浠栧�涓�牱锛岃兘鍚︽洿鏀瑰瓧绗︿覆鐨勫�锛屽彇鍐充簬鍏惰瀹氫箟涓哄父閲忚繕鏄彉閲忋�浣犱篃鍙互鍦ㄥ瓧绗︿覆鍐呮彃杩囩▼涓娇鐢ㄥ瓧绗︿覆鎻掑叆甯搁噺銆佸彉閲忋�瀛楅潰閲忚〃杈炬垚鏇撮暱鐨勫瓧绗︿覆锛岃繖鏍峰彲浠ュ緢瀹规槗鐨勫垱寤鸿嚜瀹氫箟鐨勫瓧绗︿覆鍊硷紝杩涜灞曠ず銆佸瓨鍌ㄤ互鍙婃墦鍗般�
灏界璇硶绠�槗锛屼絾String绫诲瀷鏄竴绉嶅揩閫熴�鐜颁唬鍖栫殑瀛楃涓插疄鐜般� 姣忎竴涓瓧绗︿覆閮芥槸鐢辩紪鐮佹棤鍏崇殑 Unicode 瀛楃缁勬垚锛屽苟鏀寔璁块棶瀛楃鐨勫绉�Unicode 琛ㄧず褰㈠紡锛坮epresentations锛夈�

娉ㄦ剰锛�br />Swift 鐨�code>String绫诲瀷涓�Foundation NSString绫昏繘琛屼簡鏃犵紳妗ユ帴銆傚氨鍍�nbsp;AnyObject绫诲瀷 涓彁鍒扮殑涓�牱锛屽湪浣跨敤 Cocoa 涓殑 Foundation 妗嗘灦鏃讹紝鎮ㄥ彲浠ュ皢鍒涘缓鐨勪换浣曞瓧绗︿覆鐨勫�杞崲鎴�code>NSString锛屽苟璋冪敤浠绘剰鐨�code>NSString API銆傛偍涔熷彲浠ュ湪浠绘剰瑕佹眰浼犲叆NSString瀹炰緥浣滀负鍙傛暟鐨�API 涓敤String绫诲瀷鐨勫�浠f浛銆�鏇村鍏充簬鍦�Foundation 鍜�Cocoa 涓娇鐢�code>String鐨勪俊鎭鏌ョ湅 Using Swift with Cocoa and Objective-C (Swift 2.1)銆�/p>

) 鍖呰9鐫�殑鍏锋湁鍥哄畾椤哄簭鐨勬枃鏈瓧绗﹂泦銆�瀛楃涓插瓧闈㈤噺鍙互鐢ㄤ簬涓哄父閲忓拰鍙橀噺鎻愪緵鍒濆鍊硷細

let someString = "Some string literal value" 

娉ㄦ剰someString甯搁噺閫氳繃瀛楃涓插瓧闈㈤噺杩涜鍒濆鍖栵紝Swift 浼氭帹鏂甯搁噺涓�code>String绫诲瀷銆�/p>

娉ㄦ剰锛�鏇村鍏充簬鍦ㄥ瓧绗︿覆瀛楅潰閲忎腑浣跨敤鐗规畩瀛楃鐨勪俊鎭紝璇锋煡鐪�nbsp;瀛楃涓插瓧闈㈤噺鐨勭壒娈婂瓧绗�/a> 銆�/p>

鍒濆鍖栫┖瀛楃涓�(Initializing an Empty String)

瑕佸垱寤轰竴涓┖瀛楃涓蹭綔涓哄垵濮嬪�锛屽彲浠ュ皢绌虹殑瀛楃涓插瓧闈㈤噺璧嬪�缁欏彉閲忥紝涔熷彲浠ュ垵濮嬪寲涓�釜鏂扮殑String瀹炰緥锛�/p>

var emptyString = "" // 绌哄瓧绗︿覆瀛楅潰閲� var anotherEmptyString = String() // 鍒濆鍖栨柟娉� // 涓や釜瀛楃涓插潎涓虹┖骞剁瓑浠枫� 

鎮ㄥ彲浠ラ�杩囨鏌ュ叾Boolean绫诲瀷鐨�code>isEmpty灞炴�鏉ュ垽鏂瀛楃涓叉槸鍚︿负绌猴細

if emptyString.isEmpty { print("Nothing to see here") } // 鎵撳嵃杈撳嚭锛�Nothing to see here" 

瀛楃涓插彲鍙樻� (String Mutability)

鎮ㄥ彲浠ラ�杩囧皢涓�釜鐗瑰畾瀛楃涓插垎閰嶇粰涓�釜鍙橀噺鏉ュ鍏惰繘琛屼慨鏀癸紝鎴栬�鍒嗛厤缁欎竴涓父閲忔潵淇濊瘉鍏朵笉浼氳淇敼锛�/p>

var variableString = "Horse" variableString += " and carriage" // variableString 鐜板湪涓�"Horse and carriage" let constantString = "Highlander" constantString += " and another Highlander" // 杩欎細鎶ュ憡涓�釜缂栬瘧閿欒 (compile-time error) - 甯搁噺瀛楃涓蹭笉鍙互琚慨鏀广� 

娉ㄦ剰锛�鍦�Objective-C 鍜�Cocoa 涓紝鎮ㄩ渶瑕侀�杩囬�鎷╀袱涓笉鍚岀殑绫�NSString鍜�code>NSMutableString)鏉ユ寚瀹氬瓧绗︿覆鏄惁鍙互琚慨鏀广�

缁撴瀯浣撳拰鏋氫妇鏄�绫诲瀷 涓繘琛屼簡璇︾粏鎻忚堪銆�/p>

Swift 榛樿瀛楃涓叉嫹璐濈殑鏂瑰紡淇濊瘉浜嗗湪鍑芥暟/鏂规硶涓紶閫掔殑鏄瓧绗︿覆鐨勫�銆�寰堟槑鏄炬棤璁鸿鍊兼潵鑷簬鍝噷锛岄兘鏄偍鐙嚜鎷ユ湁鐨勩� 鎮ㄥ彲浠ョ‘淇′紶閫掔殑瀛楃涓蹭笉浼氳淇敼锛岄櫎闈炰綘鑷繁鍘讳慨鏀瑰畠銆�/p>

鍦ㄥ疄闄呯紪璇戞椂锛孲wift 缂栬瘧鍣ㄤ細浼樺寲瀛楃涓茬殑浣跨敤锛屼娇瀹為檯鐨勫鍒跺彧鍙戠敓鍦ㄧ粷瀵瑰繀瑕佺殑鎯呭喌涓嬶紝杩欐剰鍛崇潃鎮ㄥ皢瀛楃涓蹭綔涓哄�绫诲瀷鐨勫悓鏃跺彲浠ヨ幏寰楁瀬楂樼殑鎬ц兘銆�/p>

for character in "Dog!馃惗".characters { print(character) } // D // o // g // ! // 馃惗

for-in寰幆鍦�nbsp;For 寰幆 涓繘琛屼簡璇︾粏鎻忚堪銆�/p>

鍙﹀锛岄�杩囨爣鏄庝竴涓�code>Character绫诲瀷骞剁敤瀛楃瀛楅潰閲忚繘琛岃祴鍊硷紝鍙互寤虹珛涓�釜鐙珛鐨勫瓧绗﹀父閲忔垨鍙橀噺锛�/p>

let exclamationMark: Character = "!" 

瀛楃涓插彲浠ラ�杩囦紶閫掍竴涓�绫诲瀷涓�code>Character鐨勬暟缁勪綔涓鸿嚜鍙橀噺鏉ュ垵濮嬪寲锛�/p>

let catCharacters: [Character] = ["C", "a", "t", "!", "馃惐"] let catString = String(catCharacters) print(catString) // 鎵撳嵃杈撳嚭锛�Cat!馃惐" 

杩炴帴瀛楃涓插拰瀛楃 (Concatenating Strings and Characters)

瀛楃涓插彲浠ラ�杩囧姞娉曡繍绠楃锛�code>+锛夌浉鍔犲湪涓�捣锛堟垨绉�ldquo;杩炴帴”锛夊垱寤轰竴涓柊鐨勫瓧绗︿覆锛�/p>

let string1 = "hello" let string2 = " there" var welcome = string1 + string2 // welcome 鐜板湪绛変簬 "hello there" 

鎮ㄤ篃鍙互閫氳繃鍔犳硶璧嬪�杩愮畻绗�(+=) 灏嗕竴涓瓧绗︿覆娣诲姞鍒颁竴涓凡缁忓瓨鍦ㄥ瓧绗︿覆鍙橀噺涓婏細

var instruction = "look over" instruction += string2 // instruction 鐜板湪绛変簬 "look over there" 

鎮ㄥ彲浠ョ敤append()鏂规硶灏嗕竴涓瓧绗﹂檮鍔犲埌涓�釜瀛楃涓插彉閲忕殑灏鹃儴锛�/p>

let exclamationMark: Character = "!" welcome.append(exclamationMark) // welcome 鐜板湪绛変簬 "hello there!" 

娉ㄦ剰锛�鎮ㄤ笉鑳藉皢涓�釜瀛楃涓叉垨鑰呭瓧绗︽坊鍔犲埌涓�釜宸茬粡瀛樺湪鐨勫瓧绗﹀彉閲忎笂锛屽洜涓哄瓧绗﹀彉閲忓彧鑳藉寘鍚竴涓瓧绗︺�

瀛楃涓叉彃鍊�(String Interpolation)

瀛楃涓叉彃鍊兼槸涓�鏋勫缓鏂板瓧绗︿覆鐨勬柟寮忥紝鍙互鍦ㄥ叾涓寘鍚父閲忋�鍙橀噺銆佸瓧闈㈤噺鍜岃〃杈惧紡銆�鎮ㄦ彃鍏ョ殑瀛楃涓插瓧闈㈤噺鐨勬瘡涓�」閮藉湪浠ュ弽鏂滅嚎涓哄墠缂�殑鍦嗘嫭鍙蜂腑锛�/p>

let multiplier = 3 let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)" // message is "3 times 2.5 is 7.5" 

鍦ㄤ笂闈㈢殑渚嬪瓙涓紝multiplier浣滀负\(multiplier)琚彃鍏ュ埌涓�釜瀛楃涓插父閲忛噺涓� 褰撳垱寤哄瓧绗︿覆鎵ц鎻掑�璁$畻鏃舵鍗犱綅绗︿細琚浛鎹负multiplier瀹為檯鐨勫�銆�/p>

multiplier鐨勫�涔熶綔涓哄瓧绗︿覆涓悗闈㈣〃杈惧紡鐨勪竴閮ㄥ垎銆�璇ヨ〃杈惧紡璁$畻Double(multiplier) * 2.5鐨勫�骞跺皢缁撴灉 (7.5) 鎻掑叆鍒板瓧绗︿覆涓� 鍦ㄨ繖涓緥瀛愪腑锛岃〃杈惧紡鍐欎负\(Double(multiplier) * 2.5)骞跺寘鍚湪瀛楃涓插瓧闈㈤噺涓�

娉ㄦ剰锛�br />鎻掑�瀛楃涓蹭腑鍐欏湪鎷彿涓殑琛ㄨ揪寮忎笉鑳藉寘鍚潪杞箟鍙嶆枩鏉�(\)锛屽苟涓斾笉鑳藉寘鍚洖杞︽垨鎹㈣绗︺�涓嶈繃锛屾彃鍊煎瓧绗︿覆鍙互鍖呭惈鍏朵粬瀛楅潰閲忋�

Unicode

Unicode 鏄竴涓浗闄呮爣鍑嗭紝鐢ㄤ簬鏂囨湰鐨勭紪鐮佸拰琛ㄧず銆�瀹冧娇鎮ㄥ彲浠ョ敤鏍囧噯鏍煎紡琛ㄧず鏉ヨ嚜浠绘剰璇█鍑犱箮鎵�湁鐨勫瓧绗︼紝骞惰兘澶熷鏂囨湰鏂囦欢鎴栫綉椤佃繖鏍风殑澶栭儴璧勬簮涓殑瀛楃杩涜璇诲啓鎿嶄綔銆�Swift 鐨�code>String鍜�code>Character绫诲瀷鏄畬鍏ㄥ吋瀹�Unicode 鏍囧噯鐨勩�

a")锛�code>U+1F425琛ㄧず灏忛浮琛ㄦ儏(FRONT-FACING BABY CHICK) ("馃惀")銆�/p>

娉ㄦ剰锛�Unicode 鐮佷綅(code poing) 鐨勮寖鍥存槸U+0000鍒�code>U+D7FF鎴栬�U+E000鍒�code>U+10FFFF銆俇nicode 鏍囬噺涓嶅寘鎷�Unicode 浠g悊椤�surrogate pair) 鐮佷綅锛屽叾鐮佷綅鑼冨洿鏄�code>U+D800鍒�code>U+DFFF銆�/p>

娉ㄦ剰涓嶆槸鎵�湁鐨�1浣�Unicode 鏍囬噺閮戒唬琛ㄤ竴涓瓧绗︼紝鍥犱负鏈変竴浜涙爣閲忔槸鐣欎綔鏈潵鍒嗛厤鐨勩�宸茬粡浠h〃涓�釜鍏稿瀷瀛楃鐨勬爣閲忛兘鏈夎嚜宸辩殑鍚嶅瓧锛屼緥濡備笂闈緥瀛愪腑鐨�code>LATIN SMALL LETTER A鍜�code>FRONT-FACING BABY CHICK銆�/p>

瀛楃涓插瓧闈㈤噺鐨勭壒娈婂瓧绗�(Special Characters in String Literals)

瀛楃涓插瓧闈㈤噺鍙互鍖呭惈浠ヤ笅鐗规畩瀛楃锛�/p>

  • 杞箟瀛楃\0(绌哄瓧绗�銆�code>\\(鍙嶆枩绾�銆�code>\t(姘村钩鍒惰〃绗�銆�code>\n(鎹㈣绗�銆�code>\r(鍥炶溅绗�銆�code>\"(鍙屽紩鍙�銆�code>\'(鍗曞紩鍙�銆�/li>
  • Unicode 鏍囬噺锛屽啓鎴�code>\u{n}(u涓哄皬鍐�锛屽叾涓�code>n涓轰换鎰忎竴鍒板叓浣嶅崄鍏繘鍒舵暟涓斿彲鐢ㄧ殑 Unicode 浣嶇爜銆�/li>

涓嬮潰鐨勪唬鐮佷负鍚勭鐗规畩瀛楃鐨勪娇鐢ㄧず渚嬨� wiseWords甯搁噺鍖呭惈浜嗕袱涓弻寮曞彿銆�code>dollarSign銆�code>blackHeart鍜�code>sparklingHeart甯搁噺婕旂ず浜嗕笁绉嶄笉鍚屾牸寮忕殑 Unicode 鏍囬噺锛�/p>

let wiseWords = "\"Imagination is more important than knowledge\" - Einstein" // "Imageination is more important than knowledge" - Enistein let dollarSign = "\u{24}" // $, Unicode 鏍囬噺 U+0024 let blackHeart = "\u{2665}" // ♥, Unicode 鏍囬噺 U+2665 let sparklingHeart = "\u{1F496}" // 馃挅, Unicode 鏍囬噺 U+1F496 

鍙墿灞曠殑瀛楀舰缇ら泦(Extended Grapheme Clusters)

姣忎竴涓�Swift 鐨�code>Character绫诲瀷浠h〃涓�釜鍙墿灞曠殑瀛楀舰缇ゃ� 涓�釜鍙墿灞曠殑瀛楀舰缇ゆ槸涓�釜鎴栧涓彲鐢熸垚浜虹被鍙鐨勫瓧绗�Unicode 鏍囬噺鐨勬湁搴忔帓鍒椼� 涓句釜渚嬪瓙锛屽瓧姣�code>é鍙互鐢ㄥ崟涓�殑 Unicode 鏍囬噺é(LATIN SMALL LETTER E WITH ACUTE, 鎴栬�U+00E9)鏉ヨ〃绀恒�鐒惰�涓�釜鏍囧噯鐨勫瓧姣�code>e(LATIN SMALL LETTER E鎴栬�U+0065) 鍔犱笂涓�釜鎬ヤ績閲嶉煶(COMBINING ACTUE ACCENT)鐨勬爣閲�U+0301)锛岃繖鏍蜂竴瀵规爣閲忓氨琛ㄧず浜嗗悓鏍风殑瀛楁瘝é銆�杩欎釜鎬ヤ績閲嶉煶鐨勬爣閲忓舰璞$殑灏�code>e杞崲鎴愪簡é銆�/p>

鍦ㄨ繖涓ょ鎯呭喌涓紝瀛楁瘝é浠h〃浜嗕竴涓崟涓�殑 Swift 鐨�code>Character鍊硷紝鍚屾椂浠h〃浜嗕竴涓彲鎵╁睍鐨勫瓧褰㈢兢銆�鍦ㄧ涓�鎯呭喌锛岃繖涓瓧褰㈢兢鍖呭惈涓�釜鍗曚竴鏍囬噺锛涜�鍦ㄧ浜岀鎯呭喌锛屽畠鏄寘鍚袱涓爣閲忕殑瀛楀舰缇わ細

let eAcute: Character = "\u{E9}" // é let combinedEAcute: Character = "\u{65}\u{301}" // e 鍚庨潰鍔犱笂 虂 // eAcute 鏄�é, combinedEAcute 鏄�é 

鍙墿灞曠殑瀛楃缇ら泦鏄竴涓伒娲荤殑鏂规硶锛岀敤璁稿澶嶆潅鐨勮剼鏈瓧绗﹁〃绀哄崟涓�殑Character鍊笺� 渚嬪锛屾潵鑷湞椴滆瀛楁瘝琛ㄧ殑闊╄闊宠妭鑳借〃绀轰负缁勫悎鎴栧垎瑙g殑鏈夊簭鎺掑垪銆�鍦�Swift 閮戒細琛ㄧず涓哄悓涓�釜鍗曚竴鐨�code>Character鍊硷細

let precomposed: Character = "\u{D55C}" // 頃� let decomposed: Character = "\u{1112}\u{1161}\u{11AB}" // 釀� 釁� 釂� // precomposed 鏄�頃� decomposed 鏄�頃� 

鍙嫇灞曠殑瀛楃缇ら泦鍙互浣垮寘鍥磋鍙�渚嬪COMBINING ENCLOSING CIRCLE鎴栬�U+20DD)鐨勬爣閲忓寘鍥村叾浠�Unicode 鏍囬噺锛屼綔涓轰竴涓崟涓�殑Character鍊硷細

let enclosedEAcute: Character = "\u{E9}\u{20DD}" // enclosedEAcute 鏄�é鈨� 

灞�儴鐨勬寚绀虹鍙风殑 Unicode 鏍囬噺鍙互缁勫悎鎴愪竴涓崟涓�殑Character鍊硷紝渚嬪REGIONAL INDICATOR SYMBOL LETTER U(U+1F1FA)鍜�code>REGIONAL INDICATOR SYMBOL LETTER S(U+1F1F8)锛�/p>

let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}" // regionalIndicatorForUS 鏄�馃嚭馃嚫 

璁$畻瀛楃鏁伴噺 (Counting Characters)

濡傛灉鎯宠鑾峰緱涓�釜瀛楃涓蹭腑Character鍊肩殑鏁伴噺锛屽彲浠ヤ娇鐢ㄥ瓧绗︿覆鐨�code>characters灞炴�鐨�code>count灞炴�锛�/p>

let unusualMenagerie = "Koala 馃惃, Snail 馃悓, Penguin 馃惂, Dromedary 馃惇" print("unusualMenagerie has \(unusualMenagerie.characters.count) characters") // 鎵撳嵃杈撳嚭 "unusualMenagerie has 40 characters" 

娉ㄦ剰鍦�Swift 涓紝浣跨敤鍙嫇灞曠殑瀛楃缇ら泦浣滀负Character鍊兼潵杩炴帴鎴栨敼鍙樺瓧绗︿覆鏃讹紝骞朵笉涓�畾浼氭洿鏀瑰瓧绗︿覆鐨勫瓧绗︽暟閲忋�

渚嬪锛屽鏋滀綘鐢ㄥ洓涓瓧绗︾殑鍗曡瘝cafe鍒濆鍖栦竴涓柊鐨勫瓧绗︿覆锛岀劧鍚庢坊鍔犱竴涓�code>COMBINING ACTUE ACCENT(U+0301)浣滀负瀛楃涓茬殑缁撳熬銆傛渶缁堣繖涓瓧绗︿覆鐨勫瓧绗︽暟閲忎粛鐒舵槸4锛屽洜涓虹鍥涗釜瀛楃鏄�code>é锛岃�涓嶆槸e锛�/p>

var word = "cafe" print("the number of characters in \(word) is \(word.characters.count)") // 鎵撳嵃杈撳嚭 "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)") // 鎵撳嵃杈撳嚭 "the number of characters in café is 4" 

娉ㄦ剰锛�鍙墿灞曠殑瀛楃缇ら泦鍙互缁勬垚涓�釜鎴栬�澶氫釜 Unicode 鏍囬噺銆傝繖鎰忓懗鐫�笉鍚岀殑瀛楃浠ュ強鐩稿悓瀛楃鐨勪笉鍚岃〃绀烘柟寮忓彲鑳介渶瑕佷笉鍚屾暟閲忕殑鍐呭瓨绌洪棿鏉ュ瓨鍌ㄣ�鎵�互 Swift 涓殑瀛楃鍦ㄤ竴涓瓧绗︿覆涓苟涓嶄竴瀹氬崰鐢ㄧ浉鍚岀殑鍐呭瓨绌洪棿鏁伴噺銆傚洜姝ゅ湪娌℃湁鑾峰彇瀛楃涓茬殑鍙墿灞曠殑瀛楃缇ょ殑鑼冨洿鏃跺�锛屽氨涓嶈兘璁$畻鍑哄瓧绗︿覆鐨勫瓧绗︽暟閲忋�濡傛灉鎮ㄦ鍦ㄥ鐞嗕竴涓暱瀛楃涓诧紝闇�娉ㄦ剰characters灞炴�蹇呴』閬嶅巻鍏ㄩ儴鐨�Unicode 鏍囬噺锛屾潵纭畾瀛楃涓茬殑瀛楃鏁伴噺銆�/p>

鍙﹀闇�娉ㄦ剰鐨勬槸閫氳繃characters灞炴�杩斿洖鐨勫瓧绗︽暟閲忓苟涓嶆�鏄笌鍖呭惈鐩稿悓瀛楃鐨�code>NSString鐨�code>length灞炴�鐩稿悓銆�code>NSString鐨�code>length灞炴�鏄埄鐢�UTF-16 琛ㄧず鐨勫崄鍏綅浠g爜鍗曞厓鏁板瓧锛岃�涓嶆槸 Unicode 鍙墿灞曠殑瀛楃缇ら泦銆備綔涓轰綈璇侊紝褰撲竴涓�code>NSString鐨�code>length灞炴�琚竴涓猄wift鐨�code>String鍊艰闂椂锛屽疄闄呬笂鏄皟鐢ㄤ簡utf16Count銆�/p>

璁块棶鍜屼慨鏀瑰瓧绗︿覆 (Accessing and Modifying a String)

浣犲彲浠ラ�瀛楃涓茬殑灞炴�鍜屾柟娉曟潵璁块棶鍜岃鍙栧畠锛屽綋鐒朵篃鍙互鐢ㄤ笅鏍囪娉曞畬鎴愩�

瀛楃涓茬储寮�(String Indices)

姣忎竴涓�code>String鍊奸兘鏈変竴涓叧鑱旂殑绱㈠紩(index)绫诲瀷锛�code>String.Index锛屽畠瀵瑰簲鐫�瓧绗︿覆涓殑姣忎竴涓�code>Character鐨勪綅缃�

鍓嶉潰鎻愬埌锛屼笉鍚岀殑瀛楃鍙兘浼氬崰鐢ㄤ笉鍚屾暟閲忕殑鍐呭瓨绌洪棿锛屾墍浠ヨ鐭ラ亾Character鐨勭‘瀹氫綅缃紝灏卞繀椤讳粠String寮�ご閬嶅巻姣忎竴涓�Unicode 鏍囬噺鐩村埌缁撳熬銆傚洜姝わ紝Swift 鐨勫瓧绗︿覆涓嶈兘鐢ㄦ暣鏁�integer)鍋氱储寮曘�

浣跨敤startIndex灞炴�鍙互鑾峰彇涓�釜String鐨勭涓�釜Character鐨勭储寮曘�浣跨敤endIndex灞炴�鍙互鑾峰彇鏈�悗涓�釜Character鐨勫悗涓�釜浣嶇疆鐨勭储寮曘�鍥犳锛�code>endIndex灞炴�涓嶈兘浣滀负涓�釜瀛楃涓茬殑鏈夋晥涓嬫爣銆傚鏋�code>String鏄┖涓诧紝startIndex鍜�code>endIndex鏄浉绛夌殑銆�/p>

閫氳繃璋冪敤String.Index鐨�code>predecessor()鏂规硶锛屽彲浠ョ珛鍗冲緱鍒板墠闈竴涓储寮曪紝璋冪敤successor()鏂规硶鍙互绔嬪嵆寰楀埌鍚庨潰涓�釜绱㈠紩銆備换浣曚竴涓�code>String鐨勭储寮曢兘鍙互閫氳繃閿侀摼浣滅敤鐨勮繖浜涙柟娉曟潵鑾峰彇鍙︿竴涓储寮曪紝涔熷彲浠ヨ皟鐢�code>advancedBy(_:)鏂规硶鏉ヨ幏鍙栥�浣嗗鏋滃皾璇曡幏鍙栧嚭鐣岀殑瀛楃涓茬储寮曪紝灏变細鎶涘嚭涓�釜杩愯鏃堕敊璇�

浣犲彲浠ヤ娇鐢ㄤ笅鏍囪娉曟潵璁块棶String鐗瑰畾绱㈠紩鐨�code>Character銆�/p>

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 

璇曞浘鑾峰彇瓒婄晫绱㈠紩瀵瑰簲鐨�code>Character锛屽皢寮曞彂涓�釜杩愯鏃堕敊璇�

greeting[greeting.endIndex] // error greeting.endIndex.successor() // error 

浣跨敤characters灞炴�鐨�code>indices灞炴�浼氬垱寤轰竴涓寘鍚叏閮ㄧ储寮曠殑鑼冨洿(Range)锛岀敤鏉ュ湪涓�釜瀛楃涓蹭腑璁块棶鍗曚釜瀛楃銆�/p>

for index in greeting.characters.indices { print("\(greeting[index]) ", terminator: " ") } // 鎵撳嵃杈撳嚭 "G u t e n T a g !" 

鎻掑叆鍜屽垹闄�(Inserting and Removing)

璋冪敤insert(_:atIndex:)鏂规硶鍙互鍦ㄤ竴涓瓧绗︿覆鐨勬寚瀹氱储寮曟彃鍏ヤ竴涓瓧绗︺�

var welcome = "hello" welcome.insert("!", atIndex: welcome.endIndex) // welcome now 鐜板湪绛変簬 "hello!" 

璋冪敤insertContentsOf(_:at:)鏂规硶鍙互鍦ㄤ竴涓瓧绗︿覆鐨勬寚瀹氱储寮曟彃鍏ヤ竴涓瓧绗︿覆銆�/p>

welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor()) // welcome 鐜板湪绛変簬 "hello there!" 

璋冪敤removeAtIndex(_:)鏂规硶鍙互鍦ㄤ竴涓瓧绗︿覆鐨勬寚瀹氱储寮曞垹闄や竴涓瓧绗︺�

welcome.removeAtIndex(welcome.endIndex.predecessor())
// welcome 鐜板湪绛変簬 "hello there" 

璋冪敤removeRange(_:)鏂规硶鍙互鍦ㄤ竴涓瓧绗︿覆鐨勬寚瀹氱储寮曞垹闄や竴涓瓙瀛楃涓层�

let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex welcome.removeRange(range) // welcome 鐜板湪绛変簬 "hello" 

姣旇緝瀛楃涓�(Comparing Strings)

Swift 鎻愪緵浜嗕笁绉嶆柟寮忔潵姣旇緝鏂囨湰鍊硷細瀛楃涓插瓧绗︾浉绛夈�鍓嶇紑鐩哥瓑鍜屽悗缂�浉绛夈�

瀛楃涓�瀛楃鐩哥瓑 (String and Character Equality)

瀛楃涓�瀛楃鍙互鐢ㄧ瓑浜庢搷浣滅(==)鍜屼笉绛変簬鎿嶄綔绗�!=)锛岃缁嗘弿杩板湪姣旇緝杩愮畻绗�/a>锛�/p>

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") } // 鎵撳嵃杈撳嚭 "These two strings are considered equal" 

濡傛灉涓や釜瀛楃涓诧紙鎴栬�涓や釜瀛楃锛夌殑鍙墿灞曠殑瀛楀舰缇ら泦鏄爣鍑嗙浉绛夌殑锛岄偅灏辫涓哄畠浠槸鐩哥瓑鐨勩�鍦ㄨ繖涓儏鍐典笅锛屽嵆浣垮彲鎵╁睍鐨勫瓧褰㈢兢闆嗘槸鏈変笉鍚岀殑 Unicode 鏍囬噺鏋勬垚鐨勶紝鍙瀹冧滑鏈夊悓鏍风殑璇█鎰忎箟鍜屽瑙傦紝灏辫涓哄畠浠爣鍑嗙浉绛夈�

渚嬪锛�code>LATIN SMALL LETTER E WITH ACUTE(U+00E9)灏辨槸鏍囧噯鐩哥瓑浜�code>LATIN SMALL LETTER E(U+0065)鍚庨潰鍔犱笂COMBINING ACUTE ACCENT(U+0301)銆傝繖涓や釜瀛楃缇ら泦閮芥槸琛ㄧず瀛楃é鐨勬湁鏁堟柟寮忥紝鎵�互瀹冧滑琚涓烘槸鏍囧噯鐩哥瓑鐨勶細

// "Voulez-vous un café?" 浣跨敤 LATIN SMALL LETTER E WITH ACUTE let eAcuteQuestion = "Voulez-vous un caf\u{E9}?" // "Voulez-vous un café?" 浣跨敤 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") } // 鎵撳嵃杈撳嚭 "These two strings are considered equal" 

鐩稿弽锛岃嫳璇腑鐨�code>LATIN CAPITAL LETTER A(U+0041锛屾垨鑰�code>A)涓嶇瓑浜庝縿璇腑鐨�code>CYRILLIC CAPITAL LETTER A(U+0410锛屾垨鑰�code>A)銆備袱涓瓧绗︾湅鐫�槸涓�牱鐨勶紝浣嗗嵈鏈変笉鍚岀殑璇█鎰忎箟锛�/p>

let latinCapitalLetterA: Character = "\u{41}" let cyrillicCapitalLetterA: Character = "\u{0410}" if latinCapitalLetterA != cyrillicCapitalLetterA { print("These two characters are not equivalent") } // 鎵撳嵃 "These two characters are not equivalent" 

娉ㄦ剰锛�鍦�Swift 涓紝瀛楃涓插拰瀛楃骞朵笉鍖哄垎鍖哄煙銆�/p>

鍓嶇紑/鍚庣紑鐩哥瓑 (Prefix and Suffix Equality)

閫氳繃璋冪敤瀛楃涓茬殑hasPrefix(_:)/hasSuffix(_:)鏂规硶鏉ユ鏌ュ瓧绗︿覆鏄惁鎷ユ湁鐗瑰畾鍓嶇紑/鍚庣紑锛屼袱涓柟娉曞潎鎺ユ敹涓�釜String绫诲瀷鐨勫弬鏁帮紝骞惰繑鍥炰竴涓竷灏斿�銆�/p>

涓嬮潰鐨勪緥瀛愪互涓�釜瀛楃涓叉暟缁勮〃绀鸿帋澹瘮浜氳瘽鍓с�缃楀瘑娆т笌鏈变附鍙躲�涓墠涓ゅ満鐨勫満鏅綅缃細

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" ] 

鎮ㄥ彲浠ヨ皟鐢�code>hasPrefix(_:)鏂规硶鏉ヨ绠楄瘽鍓т腑绗竴骞曠殑鍦烘櫙鏁帮細

var act1SceneCount = 0 for scene in romeoAndJuliet { if scene.hasPrefix("Act 1 ") { ++act1SceneCount } } print("There are \(act1SceneCount) scenes in Act 1") // 鎵撳嵃杈撳嚭 "There are 5 scenes in Act 1" 

鐩镐技鍦帮紝鎮ㄥ彲浠ョ敤hasSuffix(_:)鏂规硶鏉ヨ绠楀彂鐢熷湪涓嶅悓鍦版柟鐨勫満鏅暟锛�/p>

var mansionCount = 0 var cellCount = 0 for scene in romeoAndJuliet { if scene.hasSuffix("Capulet's mansion") { ++mansionCount } else if scene.hasSuffix("Friar Lawrence's cell") { ++cellCount } } print("\(mansionCount) mansion scenes; \(cellCount) cell scenes") // 鎵撳嵃杈撳嚭 "6 mansion scenes; 2 cell scenes" 

娉ㄦ剰锛�nbsp;hasPrefix(_:)鍜�code>hasSuffix(_:)鏂规硶閮芥槸鍦ㄦ瘡涓瓧绗︿覆涓�瀛楃姣旇緝鍏跺彲鎵╁睍鐨勫瓧绗︾兢闆嗘槸鍚︽爣鍑嗙浉绛夛紝璇︾粏鎻忚堪鍦�a href="http://www.swift51.com/swift2.1/chapter2/03_Strings_and_Characters.html#string_and_character_equality">瀛楃涓�瀛楃鐩哥瓑銆�/p>

浣跨敤瀛楃 涓繘琛屼簡鎻忚堪銆�/p>

鍙﹀锛岃兘澶熶互鍏朵粬涓夌 Unicode 鍏煎鐨勬柟寮忚闂瓧绗︿覆鐨勫�锛�/p>

  • UTF-8 浠g爜鍗曞厓闆嗗悎 (鍒╃敤瀛楃涓茬殑utf8灞炴�杩涜璁块棶)
  • UTF-16 浠g爜鍗曞厓闆嗗悎 (鍒╃敤瀛楃涓茬殑utf16灞炴�杩涜璁块棶)
  • 21浣嶇殑 Unicode 鏍囬噺鍊奸泦鍚堬紝涔熷氨鏄瓧绗︿覆鐨�UTF-32 缂栫爜鏍煎紡 (鍒╃敤瀛楃涓茬殑unicodeScalars灞炴�杩涜璁块棶)

涓嬮潰鐢�code>D``o``g``鈥�/code>(DOUBLE EXCLAMATION MARK, Unicode 鏍囬噺 U+203C)鍜�code>锟�/code>(DOG FACE锛孶nicode 鏍囬噺涓�code>U+1F436)缁勬垚鐨勫瓧绗︿覆涓殑姣忎竴涓瓧绗︿唬琛ㄧ潃涓�涓嶅悓鐨勮〃绀猴細

let dogString = "Dog鈥拣煇� 

UTF-8 琛ㄧず

鎮ㄥ彲浠ラ�杩囬亶鍘�code>String鐨�code>utf8灞炴�鏉ヨ闂畠鐨�code>UTF-8琛ㄧず銆�鍏朵负String.UTF8View绫诲瀷鐨勫睘鎬э紝UTF8View鏄棤绗﹀彿8浣�(UInt8) 鍊肩殑闆嗗悎锛屾瘡涓�釜UInt8鍊奸兘鏄竴涓瓧绗︾殑 UTF-8 琛ㄧず锛�/p>

Character D
U+0044
o
U+006F
g
U+0067
鈥�br />U+203C 馃惗
U+1F436
UTF-8
Code Unit
68 111 103 226 128 188 240 159 144 182
Position 0 1 2 3 4 5 6 7 8 9
for codeUnit in dogString.utf8 { print("\(codeUnit) ", terminator: "") } print("") // 68 111 103 226 128 188 240 159 144 182 

涓婇潰鐨勪緥瀛愪腑锛屽墠涓変釜10杩涘埗codeUnit鍊�(68111103) 浠h〃浜嗗瓧绗�code>D銆�code>o鍜�nbsp;g锛屽畠浠殑 UTF-8 琛ㄧず涓�ASCII 琛ㄧず鐩稿悓銆�鎺ヤ笅鏉ョ殑涓変釜10杩涘埗codeUnit鍊�(226128188) 鏄�code>DOUBLE EXCLAMATION MARK鐨�瀛楄妭 UTF-8 琛ㄧず銆�鏈�悗鐨勫洓涓�code>codeUnit鍊�(240159144182) 鏄�code>DOG FACE鐨�瀛楄妭 UTF-8 琛ㄧず銆�/p>

UTF-16 琛ㄧず

鎮ㄥ彲浠ラ�杩囬亶鍘�code>String鐨�code>utf16灞炴�鏉ヨ闂畠鐨�code>UTF-16琛ㄧず銆�鍏朵负String.UTF16View绫诲瀷鐨勫睘鎬э紝UTF16View鏄棤绗﹀彿16浣�(UInt16) 鍊肩殑闆嗗悎锛屾瘡涓�釜UInt16閮芥槸涓�釜瀛楃鐨�UTF-16 琛ㄧず锛�/p>

Character D
U+0044
o
U+006F
g
U+0067
鈥�br />U+203C 馃惗
U+1F436
UTF-16
Code Unit
68 111 103 8252 55357 56374
Position 0 1 2 3 4 5
for codeUnit in dogString.utf16 { print("\(codeUnit) ", terminator: "") } print("") // 68 111 103 8252 55357 56374 

鍚屾牱锛屽墠涓変釜codeUnit鍊�(68111103) 浠h〃浜嗗瓧绗�code>D銆�code>o鍜�code>g锛屽畠浠殑 UTF-16 浠g爜鍗曞厓鍜�UTF-8 瀹屽叏鐩稿悓锛堝洜涓鸿繖浜�Unicode 鏍囬噺琛ㄧず ASCII 瀛楃锛夈�

绗洓涓�code>codeUnit鍊�(8252) 鏄竴涓瓑浜庡崄鍏繘鍒�code>203C鐨勭殑鍗佽繘鍒跺�銆傝繖涓唬琛ㄤ簡DOUBLE EXCLAMATION MARK瀛楃鐨�Unicode 鏍囬噺鍊�code>U+203C銆傝繖涓瓧绗﹀湪 UTF-16 涓彲浠ョ敤涓�釜浠g爜鍗曞厓琛ㄧず銆�/p>

绗簲鍜岀鍏釜codeUnit鍊�(55357鍜�code>56374) 鏄�code>DOG FACE瀛楃鐨�UTF-16 琛ㄧず銆�绗竴涓�涓�code>U+D83D(鍗佽繘鍒跺�涓�code>55357)锛岀浜屼釜鍊间负U+DC36(鍗佽繘鍒跺�涓�code>56374)銆�/p>

Unicode 鏍囬噺琛ㄧず (Unicode Scalars Representation)

鎮ㄥ彲浠ラ�杩囬亶鍘�code>String鍊肩殑unicodeScalars灞炴�鏉ヨ闂畠鐨�Unicode 鏍囬噺琛ㄧず銆�鍏朵负UnicodeScalarView绫诲瀷鐨勫睘鎬э紝UnicodeScalarView鏄�code>UnicodeScalar鐨勯泦鍚堛�UnicodeScalar鏄�1浣嶇殑 Unicode 浠g爜鐐广�

姣忎竴涓�code>UnicodeScalar鎷ユ湁涓�釜value灞炴�锛屽彲浠ヨ繑鍥炲搴旂殑21浣嶆暟鍊硷紝鐢�code>UInt32鏉ヨ〃绀猴細

Character D
U+0044
o
U+006F
g
U+0067
鈥�br />U+203C 馃惗
U+1F436
UTF-16
Code Unit
68 111 103 8252 128054
Position 0 1 2 3 4
for scalar in dogString.unicodeScalars { print("\(scalar.value) ", terminator: "") } print("") // 68 111 103 8252 128054 

鍓嶄笁涓�code>UnicodeScalar鍊�68111103)鐨�code>value灞炴�浠嶇劧浠h〃瀛楃D銆�code>o鍜�code>g銆�绗洓涓�code>codeUnit鍊�8252)浠嶇劧鏄竴涓瓑浜庡崄鍏繘鍒�code>203C鐨勫崄杩涘埗鍊笺�杩欎釜浠h〃浜�code>DOUBLE EXCLAMATION MARK瀛楃鐨�Unicode 鏍囬噺U+203C銆�/p>

绗簲涓�code>UnicodeScalar鍊肩殑value灞炴�锛�code>128054锛屾槸涓�釜鍗佸叚杩涘埗1F436鐨勫崄杩涘埗琛ㄧず銆傚叾绛夊悓浜�code>DOG FACE鐨�Unicode 鏍囬噺U+1F436銆�/p>

浣滀负鏌ヨ瀹冧滑鐨�code>value灞炴�鐨勪竴绉嶆浛浠f柟娉曪紝姣忎釜UnicodeScalar鍊间篃鍙互鐢ㄦ潵鏋勫缓涓�釜鏂扮殑String鍊硷紝姣斿鍦ㄥ瓧绗︿覆鎻掑�涓娇鐢細

for scalar in dogString.unicodeScalars { print("\(scalar) ") } // D // o // g // 鈥� // 馃惗 

 

你可能感兴趣的:(Swift-2.3瀛楃涓插拰瀛楃)