How can you learn a new language with fewer strings? 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//function to print variable address 2 func printvaraddress (tempstring:string) {3 4 var address = String (format: "%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 strtemp = "Ludashi" var strtempcopy = strtempprintvaraddress (strtemp)//--0x100525d50printvaraddress (strTempCopy)/ /--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 myfirstname = "Li" var mysecondname = "Zelu" var myName = mysecondname + MYFIRSTNAMEP RINTLN (MyName)//--Zeluli
Three, String traversal
Strings in Swift can be traversed directly using for-in, as follows:
============== string traversal ==========var searchstring = "Ludashi" for Tempchar in 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 mynametemp = "Lizelu" var myblogname = "Ludashi" var boolone = Mynametemp = myblogname//--Falsevar bool Double = 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//judgment prefix or suffix 2 var ishasprefixorsuffix = "I am Lizelu" 3 4 var isprefix = Ishasprefixorsuffix.hasprefix ("i") 5 println (Isprefix )//--Ture6 7 var issuffix = Ishasprefixorsuffix.hassuffix ("Zelu") 8 println (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 Swift you can use \ (), as shown here:
1//String interpolation 2 var inserttostringvalue = 10101013 4 var strinserreaultvalue = "binary encoding \ (inserttostringvalue)" 5 6 println (strinse Rreaultvalue)//binary code 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 turn into Nssting call nssting Method 2 var stringtonsstring = "Swiftwithme" 3 4 var strns:nsstring = "AAA" 5 6 strns.length//- -3--7 8//Can drop yo nssting of the various types of methods 9 var strlength = (stringtonsstring as nsstring). Length//-11--
Today's things about swift strings come here first.
Swift Strings (String)