Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the string types and common methods in Swift

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)..

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report