Swift Learning Notes (16)--string The underlying operation of a string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.