Detail the Characters character type and String string type in Swift

  • 2020-05-19 06:00:36
  • OfStack

1. The introduction

Swift provides String types and Characters types to handle string and character data. String types in Swift provide many methods for developers to use and can also be converted to NSString classes in Foundation framework, which is 10 points easier to use.

2. String basis

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


let str = "Hello, playground"

You can create an empty string in two ways:


let str1 = ""
let str2 = String()

The isEmpty method is called to determine if a string is empty. This method returns an Bool value, which can be used directly in the if statement:


if str1.isEmpty {
  print("this String Object is Empty")
}

Unlike Objective-C, which is different from NSString and NSMutableString, in Swift, if you need to create a mutable string, you simply use a variable to receive it:


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

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


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

Get the length of the string using the following code:


str3.characters.count

String in Swift can be compared directly using the == operator, as shown below:


let comStr1 = "one two"
let comStr2 = "one two"
comStr1==comStr2//true

The following code is used to verify that a string contains a prefix and suffix:


let tmp3 = "thank you"
tmp3.hasPrefix("thank")//true
tmp3.hasSuffix("you")//true

3. Use of Character

Character is the character type in Swift. In the for-in loop, all the characters in the string can be traversed:


for chara in str3.characters {
  print(chara)
}

You can also create a separate character type quantifier, as shown below:


let char1 = ""
var cgar2 = "HS"

In fact, the Sting string can also be initialized with an Character character array:


let str1 = ""
let str2 = String()
0

Append a character to a string using the following method:


let str1 = ""
let str2 = String()
1

4. Special characters in a string

Special characters in strings mainly refer to escape characters. Escape characters in Swift are listed as follows:


let str1 = ""
let str2 = String()
2

5. About string subscripts

In Swift, strings can also be submarked to access the characters, and a related method is provided to facilitate the subscript movement. The sample code is as follows:


let tmp = "Hello Swift"
// Gets the value of the lower label at the beginning of a character  0
let indexStart = tmp.startIndex
// After getting some index 1 The subscript of  1
let next = indexStart.successor()
// To obtain the final 1 The value of the following characters   Note that there are \0 The presence of  
let indexEnd = tmp.endIndex
// Before getting some index 1 The subscript of 
let pre = indexEnd.predecessor()
// Gets the characters in a string by subscript  t
var c = tmp[pre]
// I'm going to move the subscript  o
var c2 = tmp[indexStart.advancedBy(4)]
// Traversing a character by traversing the subscript  H e l l o S w i f t
for index in tmp.characters.indices {
   print("\(tmp[index]) ", terminator: "")
}

6. Inserts and removes characters from a string

Insert a character into the string using the insert function, as shown below:


let str1 = ""
let str2 = String()
4

Note that the insert function in the sample code above can only be used to insert 1 character. If you need to insert 1 group of characters, you need to use the following method:


let str1 = ""
let str2 = String()
5

Use the removeAtIndex function to remove 1 character from a string, as shown below:


let str1 = ""
let str2 = String()
6

If you want to remove a group of characters, Range is the main way to do so. An example is as follows:


let str1 = ""
let str2 = String()
7


Related articles: