String as a very important data type, there are many methods of operation. We hit explain.
(1) string interpolation: string interpolation
Different data types can be output as a string type at print time, and can be output using the "\ (variable)" method. The code is as follows: It can be found that in Swift \ n is also a newline. In the example, the most important types of data in Swift are used.
var siint:int = 2;var sidouble:double = 3.1415926var Sibool:bool = Truevar sistring:string = "Hello" var situple: (Int,int) = (2,4) var sioptional:int? = 23var Sicharacter:character = "H" println ("int=\ (siint) \ndouble=\ (sidouble) \nbool=\ (sibool) \nstring=\ (sistring) \ Ntuple=\ (situple) \noptional=\ (sioptional!) \ncharacter=\ (sicharacter) ")
Output Result:
。
(2) string comparing: string comparison
The comparison of strings in Swift is also the same as in other languages, using dictionary ordering comparisons. The dictionary is larger after the order. The comparisons here are made by logical operators such as "= =", ">", "<", ">=", "<=", "! =". Returns a Boolean value. The code is as follows:
var str1:string = "abc" var str2:string = "abd" str1 = = Str2str1 < STR2STR1 > str2str1! = str2
The output results are as follows:
。
(3) String Prefix; String Suffix, strings prefix, string suffix
These two methods are similar to the Startwith (), Endwith () method in Java. Can be used to check whether a string contains a prefix, or a suffix. Looking at an example, I need to retrieve several elements in the array that begin with "a".
Let arraytest = ["abc", "Bhe", "AfDF", "123", "Frgfs", "4223523", "2", "DFSG"]var count:int = 0for index in arraytest{ if Index.hasprefix ("a") { count++ }}println (count)
Output: Based on the result, the Hasprefix () method successfully determines the string containing the prefix "a". In line with our expectations. The Hassuffix () method is similar.
。
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift Learning Notes (16)--string The underlying operation of a string