Swift Characters: In this tutorial, you will learn about Swift characters and strings. You’ll additionally learn various operations that can be performed on strings and characters.
For each programming languages, characters and its combination, i.e., strings assume a huge part in programming and bring with it some striking highlights that assist programmers to shape messages and statements inside the program. A string is a sequence of character. These strings and characters are used to show a message using println statement in Swift as you have seen earlier.
Character is an data type that addresses a single-character string (“a”, “@”, “5”, and so on)
We use the Character watchword to create character-type variables in Swift. For instance,
var letter: Character
Here, the letter variable can only store single-character data.
// create character variable var letter: Character = "H" print(letter) // H var symbol: Character = "@" print(symbol) // @
In the above example, we have created two character variables: letter and symbol. Here, we have assigned”H” to letter and “@” to the symbol.
Note: If we attempt to assign more than one character to a Character variable, we will get an error.
// create character variable let test: Character = "H@" print(test) // Error: // cannot convert value of type 'String' to specified type Character
In Swift, a string is used to store textual data (“Hey There!”, “Swift is awesome.”, and so forth)
We use the String catchphrase to create string-type variables. For instance,
let name: String
Here, the name variable can just store textual data.
Note: Since a string contains multiple characters, it is known as a sequence of characters.
// create string type variables let name: String = "Swift" print(name) let message = "I love Swift." print(message)
Swift I love Swift.
In the above example, we have created string-type variables: name and message with values “Swift” and “I love Swift” respectively.
Notice the statement,
let message = "I love Swift."
Here, we haven’t used the String keyword while creating the variable. It is on the grounds that Swift is able to infer the type based on the value.
Note: In Swift, we use twofold quotes to address strings and characters.
The String class in Swift provides different built-in functions that allow us to perform various procedure on strings.
1. Compare Two Strings
We use the == operator to compare two strings. In the event that two strings are equivalent, the operator brings true back. Else, it returns false. For instance,
let str1 = "Hello, world!" let str2 = "I love Swift." let str3 = "Hello, world!" // compare str1 and str2 print(str1 == str2) // compare str1 and str3 print(str1 == str3)
In the above example,
- str1 and str2 are not equivalent. Thus, the outcome is false.
- str1 and str3 are equivalent. Thus, the outcome is true.
2. Join Two Strings
We use the append() function to join two strings in Swift. For instance,
var greet = "Hello " var name = "Salman" // using the append() method greet.append(name) print(greet)
In the above example, we have used the append() method to join name and greet.
Concatenate Using + and +=
We can likewise use the + and += operators to connect two strings.
var greet = "Hello, " let name = "Salman" // using + operator var result = greet + name print(result) //using =+ operator greet += name print(greet)
Hello, Salman Hello, Salman
In the above example, we have used the + and += operators to join two strings: greet and name.
Note: We can’t create greet using let. It is on the grounds that the += operator joins two strings and assigns the new value to greet.
3. Find Length of String
We use the count property to find the length of a string. For instance,
let message = "Hello, World!" // count length of a string print(message.count) // 13
Note: The count property counts the total number of characters in a string including whitespaces.
Other Built-in Functions
|isEmpty||determines if a string is empty or not|
|capitalized||capitalizes the first letter of every word in a string|
|uppercased()||converts string to uppercase|
|lowercase()||converts string to lowercase|
|hasPrefix()||determines if a string starts with certain characters or not|
|hasSuffix()||determines if a string ends with certain characters or not|
The escape sequence is used to escape some of the characters present inside a string.
Assume we need to incorporate twofold quotes inside a string.
// include double quote var example = "This is "String" class" print(example) // throws error
Since strings are addressed by double quotes, the compiler will treat “This is ” as the string. Consequently, the above code will cause an error.
To solve this issue, we use the escape character \ in Swift.
// use the escape character var example = "This is \"String\" class" print(example) // Output: This is "String" class
Presently the program will run with no error. Here, the escape character will tell the compiler to overlook the character after \ .
Here is a list of all the escape sequences supported by Swift.
|\t||a horizontal tab|
We can likewise use the backslash character \ to use variables and constants inside a string. For instance,
let name = "Swift" var message = "This is \(name) programming." print(message)
This is Swift programming.
In the above example, notice the line
var message = "This is \(name) programming."
Here, we are using the name variable inside the string message. This process is called String Interpolation in Swift.
Swift Multiline String
We can likewise create a multiline string in Swift. For this, we use triple twofold statements “””. For instance,
// multiline string var str: String = """ Swift is awesome I love Swift """ print(str)
Swift is awesome I love Swift
In the above example, anything inside the enclosing triple-quotes is one multiline string.
Note: Multi-line strings should consistently start on another line. Else, it will create an error.
// error code var str = """Swift I love Swift """
Create String Instance
We can likewise create a string using an initializer syntax. For instance,
var str = String()
Here, the initializer syntax String() will create an empty string.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.