Explain the characters character type and string string type in Swift _swift

Source: Internet
Author: User

First, the introduction

In swift, string types and characters types are provided to handle strings and character data, and the string type in Swift can be converted to the NSString class of the foundation framework in addition to a number of methods that are easy for developers to use. It is very convenient to use.

Second, string basis

In Swift, using double quotes to define strings, developers can create a string constant with the following code:

Let str = "Hello, playground"

There are two ways to create an empty string:

Let str1 = "" Let
str2 = String ()

Call the IsEmpty method to determine whether a string is an empty string, and this method returns a bool value that can be used directly for an IF statement:

If Str1.isempty {
  print ("This String Object is Empty")
}

Unlike Objective-c, where there is a difference between nsstring and nsmutablestring, in Swift, if you need to create a mutable string, you simply use a variable to receive:

var str3 = "Hello"
str3 + + "+" world "//STR3 = Hello World

String can also use interpolation to construct a new string, using the method of \ () to write the interpolated expression in parentheses, as shown in the following example:

Let multiplier = 3 Let message
= "\ (multiplier) times 2.5 are \ (Double (multiplier) * 2.5)"//3 times 2.5 is 7.5

Gets the length of the string using the following code:

Str3.characters.count

The string in swift can be compared directly using the = = operator, as shown in the following example:

Let comStr1 = "One Two" let
comStr2 = "One Two"
comstr1==comstr2//true

The code for the following example verifies that a string contains a prefix and a suffix:

Let Tmp3 = ' Thank you '
Tmp3.hasprefix ("thank")//true Tmp3.hassuffix
("You")//true

Third, the use of character

Character is the character type in Swift, in the for-in loop, you can iterate through all the characters in the string:

For Chara in str3.characters {
  print (Chara)
}

You can also create a separate measure of character type, as shown in the following example:

Let char1 = ""
var cgar2 = "HS"

In fact, the sting string can also be initialized by a character character array:

Let chars:[character] = ["H", "E", "L", "L", "O"] let
STR4 = String (chars)

Appending characters to a string uses the following method:

var STR5 = "" Let
ca:character = "a"
Str5.append (CA)

Iv. special characters in a string

Special characters in a string are essentially escape characters, and the escape characters in Swift are listed as follows:

"I"//"" "Blank
" \ "\"/"\" backslash symbol
"T"//"tab
\ n"//line feed
"\ r"//carriage return
"\"//"" single quote
"\" "//" " "Double quote
" \u{24} "//" $ "Unicode character

Five, about the string subscript

In swift, strings can also be used to access the characters in the subscript, and provide a way to move the subscript easily, as shown in the sample code:

Let tmp = "Hello Swift"
//Get the subscript value of the character start of the
indexstart = Tmp.startindex
//Get subscript 1 of one character after a subscript =
index 0 Start.successor ()
//Get the subscript value of the last character note the existence of a "let 
indexend = Tmp.endindex
//Get subscript let pre of a character before
= Indexend.predecessor ()//
get the character in the string by subscript t
var c = tmp[pre]
//subscript move o
var c2 = tmp[ Indexstart.advancedby (4)]
//by traversing the subscript to traverse the character H e l o S w I f t for the
index in tmp.characters.indices {
   print ( Tmp[index]) ", Terminator:" ")
}

Inserting and removing characters in a string

Use the Insert function to insert a character into the string, as shown in the following example:

var tmp2 = "Hello"
tmp2.insert ("!", AtIndex:tmp2.endIndex)

Note that the Insert function in the example code above can only be used to insert a character, and if you need to insert a set of characters, you need to use the following method:

Tmp2.insertcontentsof ("Swift". Characters, At:tmp2.endIndex)

Use the Removeatindex function to remove one character from a string, as shown in the following example:

Tmp2.removeatindex (Tmp2.endIndex.predecessor ())

If you want to remove a set of characters, mainly through the range, the example is as follows:

Let range = Tmp2.endIndex.advancedBy (-4). <tmp2.endindex
Tmp2.removerange (range)

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.