鏈〉鍖呭惈鍐呭锛�/p>
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>
let someString = "Some string literal value"
娉ㄦ剰someString
甯搁噺閫氳繃瀛楃涓插瓧闈㈤噺杩涜鍒濆鍖栵紝Swift 浼氭帹鏂甯搁噺涓�code>String绫诲瀷銆�/p>
娉ㄦ剰锛�鏇村鍏充簬鍦ㄥ瓧绗︿覆瀛楅潰閲忎腑浣跨敤鐗规畩瀛楃鐨勪俊鎭紝璇锋煡鐪�nbsp;瀛楃涓插瓧闈㈤噺鐨勭壒娈婂瓧绗�/a> 銆�/p>
瑕佸垱寤轰竴涓┖瀛楃涓蹭綔涓哄垵濮嬪�锛屽彲浠ュ皢绌虹殑瀛楃涓插瓧闈㈤噺璧嬪�缁欏彉閲忥紝涔熷彲浠ュ垵濮嬪寲涓�釜鏂扮殑String
瀹炰緥锛�/p>
var emptyString = "" // 绌哄瓧绗︿覆瀛楅潰閲� var anotherEmptyString = String() // 鍒濆鍖栨柟娉� // 涓や釜瀛楃涓插潎涓虹┖骞剁瓑浠枫�
鎮ㄥ彲浠ラ�杩囨鏌ュ叾Boolean
绫诲瀷鐨�code>isEmpty灞炴�鏉ュ垽鏂瀛楃涓叉槸鍚︿负绌猴細
if emptyString.isEmpty { print("Nothing to see here") } // 鎵撳嵃杈撳嚭锛�Nothing to see here"
鎮ㄥ彲浠ラ�杩囧皢涓�釜鐗瑰畾瀛楃涓插垎閰嶇粰涓�釜鍙橀噺鏉ュ鍏惰繘琛屼慨鏀癸紝鎴栬�鍒嗛厤缁欎竴涓父閲忔潵淇濊瘉鍏朵笉浼氳淇敼锛�/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)鏉ユ寚瀹氬瓧绗︿覆鏄惁鍙互琚慨鏀广�
Swift 榛樿瀛楃涓叉嫹璐濈殑鏂瑰紡淇濊瘉浜嗗湪鍑芥暟/鏂规硶涓紶閫掔殑鏄瓧绗︿覆鐨勫�銆�寰堟槑鏄炬棤璁鸿鍊兼潵鑷簬鍝噷锛岄兘鏄偍鐙嚜鎷ユ湁鐨勩� 鎮ㄥ彲浠ョ‘淇′紶閫掔殑瀛楃涓蹭笉浼氳淇敼锛岄櫎闈炰綘鑷繁鍘讳慨鏀瑰畠銆�/p>
鍦ㄥ疄闄呯紪璇戞椂锛孲wift 缂栬瘧鍣ㄤ細浼樺寲瀛楃涓茬殑浣跨敤锛屼娇瀹為檯鐨勫鍒跺彧鍙戠敓鍦ㄧ粷瀵瑰繀瑕佺殑鎯呭喌涓嬶紝杩欐剰鍛崇潃鎮ㄥ皢瀛楃涓蹭綔涓哄�绫诲瀷鐨勫悓鏃跺彲浠ヨ幏寰楁瀬楂樼殑鎬ц兘銆�/p>
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!馃惐"
瀛楃涓插彲浠ラ�杩囧姞娉曡繍绠楃锛�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!"
娉ㄦ剰锛�鎮ㄤ笉鑳藉皢涓�釜瀛楃涓叉垨鑰呭瓧绗︽坊鍔犲埌涓�釜宸茬粡瀛樺湪鐨勫瓧绗﹀彉閲忎笂锛屽洜涓哄瓧绗﹀彉閲忓彧鑳藉寘鍚竴涓瓧绗︺�
瀛楃涓叉彃鍊兼槸涓�鏋勫缓鏂板瓧绗︿覆鐨勬柟寮忥紝鍙互鍦ㄥ叾涓寘鍚父閲忋�鍙橀噺銆佸瓧闈㈤噺鍜岃〃杈惧紡銆�鎮ㄦ彃鍏ョ殑瀛楃涓插瓧闈㈤噺鐨勬瘡涓�」閮藉湪浠ュ弽鏂滅嚎涓哄墠缂�殑鍦嗘嫭鍙蜂腑锛�/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 鏄竴涓浗闄呮爣鍑嗭紝鐢ㄤ簬鏂囨湰鐨勭紪鐮佸拰琛ㄧず銆�瀹冧娇鎮ㄥ彲浠ョ敤鏍囧噯鏍煎紡琛ㄧず鏉ヨ嚜浠绘剰璇█鍑犱箮鎵�湁鐨勫瓧绗︼紝骞惰兘澶熷鏂囨湰鏂囦欢鎴栫綉椤佃繖鏍风殑澶栭儴璧勬簮涓殑瀛楃杩涜璇诲啓鎿嶄綔銆�Swift 鐨�code>String鍜�code>Character绫诲瀷鏄畬鍏ㄥ吋瀹�Unicode 鏍囧噯鐨勩�
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>
瀛楃涓插瓧闈㈤噺鍙互鍖呭惈浠ヤ笅鐗规畩瀛楃锛�/p>
\0
(绌哄瓧绗�銆�code>\\(鍙嶆枩绾�銆�code>\t(姘村钩鍒惰〃绗�銆�code>\n(鎹㈣绗�銆�code>\r(鍥炶溅绗�銆�code>\"(鍙屽紩鍙�銆�code>\'(鍗曞紩鍙�銆�/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
姣忎竴涓�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 鏄�馃嚭馃嚫
濡傛灉鎯宠鑾峰緱涓�釜瀛楃涓蹭腑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>
浣犲彲浠ラ�瀛楃涓茬殑灞炴�鍜屾柟娉曟潵璁块棶鍜岃鍙栧畠锛屽綋鐒朵篃鍙互鐢ㄤ笅鏍囪娉曞畬鎴愩�
姣忎竴涓�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 !"
璋冪敤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"
Swift 鎻愪緵浜嗕笁绉嶆柟寮忔潵姣旇緝鏂囨湰鍊硷細瀛楃涓插瓧绗︾浉绛夈�鍓嶇紑鐩哥瓑鍜屽悗缂�浉绛夈�
瀛楃涓�瀛楃鍙互鐢ㄧ瓑浜庢搷浣滅(==
)鍜屼笉绛変簬鎿嶄綔绗�!=
)锛岃缁嗘弿杩板湪姣旇緝杩愮畻绗�/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>
閫氳繃璋冪敤瀛楃涓茬殑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>
鍙﹀锛岃兘澶熶互鍏朵粬涓夌 Unicode 鍏煎鐨勬柟寮忚闂瓧绗︿覆鐨勫�锛�/p>
utf8
灞炴�杩涜璁块棶)utf16
灞炴�杩涜璁块棶)unicodeScalars
灞炴�杩涜璁块棶)涓嬮潰鐢�code>D``o``g``鈥�/code>(DOUBLE EXCLAMATION MARK
, Unicode 鏍囬噺 U+203C
)鍜�code>锟�/code>(DOG FACE
锛孶nicode 鏍囬噺涓�code>U+1F436)缁勬垚鐨勫瓧绗︿覆涓殑姣忎竴涓瓧绗︿唬琛ㄧ潃涓�涓嶅悓鐨勮〃绀猴細
let dogString = "Dog鈥拣煇�
鎮ㄥ彲浠ラ�杩囬亶鍘�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
鍊�(68
, 111
, 103
) 浠h〃浜嗗瓧绗�code>D銆�code>o鍜�nbsp;g
锛屽畠浠殑 UTF-8 琛ㄧず涓�ASCII 琛ㄧず鐩稿悓銆�鎺ヤ笅鏉ョ殑涓変釜10杩涘埗codeUnit
鍊�(226
, 128
, 188
) 鏄�code>DOUBLE EXCLAMATION MARK鐨�瀛楄妭 UTF-8 琛ㄧず銆�鏈�悗鐨勫洓涓�code>codeUnit鍊�(240
, 159
, 144
, 182
) 鏄�code>DOG FACE鐨�瀛楄妭 UTF-8 琛ㄧず銆�/p>
鎮ㄥ彲浠ラ�杩囬亶鍘�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
鍊�(68
, 111
, 103
) 浠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>
鎮ㄥ彲浠ラ�杩囬亶鍘�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鍊�68
, 111
, 103
)鐨�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 // 鈥� // 馃惗