Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the string types and common methods in Swift

Shulou Source: shulou.com Published: 2022-06-02 17:02:45 10月03日 Update

This article mainly introduces the string types and commonly used methods in Swift, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Construct / / directly assign text = "" / / 1. Construction method text = String () / / "" / / string construction text = String ("William") / / "William" / / integer construction text = String / / "888" / / floating point construction text = String / / "8.88" / / character construction text = String ("a") / / "a" / / Boolean construction text = String (true) / / "true" / / tuple construction text = String (describing: (5) ) / / "(5,5.0, true)" / / list construction text = String (describing: [1,2,3,4]) / / "[1,2,3,4]" / / format string text = String (format: "Hello,% @", "William") / / "Hello, William" 2. Splice var text1 = "hello," var text2 = "William" var result = text1 + text2print (result) / / hello, William// 3. Insert a simple value in a string using\ (), similar to ${} var target = "Hello,\ (66)" / / Hello in Kotlin, 66var target2 = "Hello,\ (text2)" / / Hello, Williamvar target3 = "Hello,\ (1 + 2)" / / hellow, 33. Character var char: Character = "e" / / "e" var char2 = Character ("e") / / "e" / / occupies 16 bytes var size = MemoryLayout.size / / 16 hand / character array var array: [Character] = ["W", "I", "l", "l", "I", "a" "m"] / / construct Stringvar newStr = String (array) / / William// traversal string let city = "Shanghai" for cha in city {print (cha)} 4. Escape character

Escape characters are often used in typesetting

/ /\ 0: for blank / /\\: for backslash / /\ t: for tab / /\ n: for line feed / /\ r: for carriage return / /\': for single quotation marks / /\ u {}: create characters var code = "\ u {0068}\ u {0065}\ u {006c}\ u {006c}\ u {006f } "/ / hellovar escapeChar =" blanks:\ 0 backslash:\\ tab:\ t newline\ ncarriage return:\ r single quotation marks:\ 'double quotes:\ "print (escapeChar) 5. Common methods / / string null var emptyStr = "" if emptyStr.isEmpty {print ("string is empty")} if emptyStr.count = = 0 {print ("string count is 0")} / / string size is compared one by one, let str1 = "100a", str2 = "101a" if (str1 < str2) {print ("str1 < str2")} / / string equal, all characters are compared to be equal Is the equivalent string if (str1 = = str2) {print ("str1 < str2")} / / using the subscript access character var indexStr = "Hello" William "/ / get the starting subscript var startIndex: String.Index = indexStr.startIndexvar endIndex: String.Index = indexStr.endIndex// get the character var afterChar = indexStr [indexStr.index (after: startIndex)] / / edeband / get the character var beforeChar = indexStr [indexStr.index (before: endIndex)] / / var beforeChar / corresponding to the subscript before a subscript. Operator to specify a range, move 4 bits backward from startIndex to intercept substring var subStr = indexStr [startIndex...indexStr.index (startIndex, offsetBy: 4)] / / hello// move forward from endIndex 7 bit truncated substring var subStr2 = indexStr [indexStr.index (endIndex, offsetBy:-7)..

Tags: Character string subscript method quotation mark article scope commonly used position size type tab single newline slash white space escape tabulation move Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Redmi Apple Xiaomi