Spy on Swift's Strings (string)

Source: Internet
Author: User

Before summing up the string in Objective-c "objective-c Select string processing method ", learn a new language how can be less of the string it. The string in swift and the Objective-c language in the NSString are still different, and the string in swift returns to normal, which is easier and quicker to use. The topic of this blog is that the string type in Swift string,string a lot of hassle in swift. Today, this blog is a good way to get to know the string in Swift.

One, a string copy

A string copy in Swift can be manipulated directly using the = sign, which is not as simple as assigning values between pointers. If you assign the value of string A to string B, then the memory addresses of a and B are different, that is, string A and string B have their own memory space. The following example shows us the above statement:

1. First write an input function, which is used to output the memory address of the string, the code is as follows:

1 // functions for printing variable addresses 2 func printvaraddress (tempstring:string) {3     4     var " %p " , tempstring) 5     6     println (address)7     8 }

2. Create a string variable and assign an initial value, then define a variable, assign the value of the previous variable to the new variable by means of the = sign, and call the method above to print the memory address of the two variables, as shown in the following code:

var " Ludashi " var strtempcopy = strtempprintvaraddress (strtemp)  //--0X100525D50//  --0x1005268a0

As can be seen from the variable address printed above, each variable has its own storage address, which corresponds to a deep copy in OC.

Second, string connection

The connection of strings in Swift is a lot simpler, a + number is done, and no more nsstringformat, the following code is used to concatenate strings in Swift, as is the case with other programming languages such as PHP. Nonsense less to say directly on the code.

// ============= string Connection ============== var " Li " var " Zelu " var myName = mysecondname +//  --Zeluli

Three, String traversal

Strings in Swift can be traversed directly using for-in, as follows:

// ============== string Traversal ========== var " Ludashi "  for inch searchstring {    println (Tempchar)}

Iv. Comparison of strings

The comparison between strings in Word Swift is not using the Isequaltostring method, and using the = = and! = Numbers directly is not an instant simple. It should be noted that the value of type bool in Swift is no longer the yes or no in OC, but false Or ture. The following code snippet compares two strings by = = and! =.

// string comparison = = with! = var " Lizelu " var " Ludashi " var boolone = Mynametemp = = Myblogname  //  --Falsevar booltwo = Mynametemp! = Myblogname  //  --Trueprintln (boolone) println (booltwo)

V. Common string Functions in Swift

1. Use Hasprefix and Hassuffix to determine whether a string is a prefix or suffix of another string

1 //To determine the prefix or suffix2 varIshasprefixorsuffix ="I'm Lizelu ."3 4 varIsprefix = Ishasprefixorsuffix.hasprefix ("I'm")5println (Isprefix)//--Ture6 7 varIssuffix = Ishasprefixorsuffix.hassuffix ("Zelu")8println (Issuffix)//--Ture

2. String length

Getting the string length in OC is using length, whereas Swift uses the count () global function, as follows:

// string Length var strlenght = count (ishasprefixorsuffix) println (strlenght)  //  --8--

3. String interpolation

In OC If you want to insert a value into a string, you have to use the Format function of the string, and in OC you can use \ (), as follows:

1 // string Interpolation 2 var 1010101 3 4 var " Binary encoding \ (inserttostringvalue) " 5 6 println (strinserreaultvalue)  // binary coded 1010101

4. Call the NSString method

What if you want to invoke NSString's unique method in Swift? Then use the AS keyword to convert the type, that is, the string type through the as operation, converted to the NSString type, and then call NSString the appropriate method (such as to get a string specified in the range of strings, The method of using NSString is much simpler).

1 //string to Nssting call nssting method2 varStringtonsstring ="Swiftwithme"3 4 varStrns:nsstring ="AAA"5 6Strns.length//--3--7 8 //can drop yo-nssting of the various types of methods9 varStrlength = (stringtonsstring asNSString). length// --one--

Today, the Swift string is the first thing to come here, after the word. When using Swift as a development instance, the swift string is added.

Spy on Swift's Strings (string)

Related Article

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.