Details and examples of type swift Character

  • 2020-05-24 05:50:52
  • OfStack

swift Character type details and examples

1. Traverse the string


// for-in  traverse String
for c in str.characters{
  print(c)
}
//c is Character type 

2. Character statement


// Character and String Reciprocal conversion 
//let mark = "!" // At this time mark is String type 
let mark: Character = "!" //Character The declaration must be displayed 
//str + mark // Different types of stitching will report errors 
str + String(mark) // Type conversion followed by splicing 

// append  The equivalent of  +=
str.append(mark)
//var Type can be called append Method, let Type not allowed 

3. Features of type Character


// Characters Is based on Unicode the 
let englishLetter: Character = "a"
let chineseLetter: Character = " ha "
let dog: Character = "��"
let coolGuy = "\u{1F60E}"

Unlike other languages, englishLetter, chineseLetter, dog, coolGuy all have a single Character of 1


// String right Unicode The support of 
var coolLetters = "abc" //abc
coolLetters.characters.count //3

var chineseLetters = " how are you doing " // how are you doing 
chineseLetters.characters.count //3

var coolGuys = "\u{1F60E}\u{1F60E}\u{1F60E}" //������
coolGuys.characters.count //3

Swift's Character is very intelligent and makes it easy for humans to understand the characters


var cafe = "caf e " //caf e 
var cafe2 = "cafe\u{0301}" //caf e 
cafe.characters.count //4
cafe2.characters.count //4


// String The comparison of 
cafe == cafe2

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: