![]() |
VOOZH | about |
A string is a sequence of characters that is either composed of a literal constant or the same kind of variable. For eg., "Hello World" is a string of characters. In Swift4, a string is Unicode correct and locale insensitive. We are allowed to perform various operations on the string like comparison, concatenation, iteration, and many more.
We can create a string either by using a string instance or creating an instance of a string class. The following are the commonly used methods to create a string in Swift:
To create a string we can use a String() initializer. It will create an instance of a string.
Syntax:
var str = String("Hello GFG)
Example:
Output:
Hello Everyone!!!
Here, we use double apostrophes on the words for the formation of strings, similar as, normal strings in other languages. Or we can use a String data type to create a string type variable.
Syntax:
var str = " Welcome"
var s:String = "Hello"
Example:
Output:
Hello Everyone Welcome Everyone
Apart from single-line strings, we can also create multi-line strings. For the creation of multi-line strings, instead of double quotes, we have to use triple quotes at the end and start of the string(""").
Syntax:
var str = """
Hello world
How are you ?
"""
Example:
Output:
Hello People!!! This is an example of multiline string
Now, we will see some commonly used functions and operators related to a string:
Empty string is a type of string whose length is equal to zero. Or in other words, it's a sequence of zero characters in a string. For creating an empty string, either we can create a string literal with single quotes only or by passing an argument with no values in it. Sometimes people get confused between an empty string and a null string. Null string means which has no value at all in it. In other words, it can also be defined as the absence of a string instance.
Example:
Swift provides an inbuilt property to check whether the given string is empty or not. It will return true if the given string is empty. Otherwise, it will return false.
Syntax:
str.isEmpty
Example:
Output:
str1 is empty str2 is not empty
The length of a string can be defined as the total number of characters present in the string. The Count property is used for counting the length of the string. The length of an Empty string is zero.
Syntax:
str.count
Example:
Output:
Length of str is 25
Concatenation is the addition of one or more strings to the end of another string. For string literals and string constants, concatenation occurs at compile time. For string variables, concatenation occurs only at run time. For the concatenation of strings, we have to use the '+' operator to concatenate either strings or strings, or strings and characters, or characters and characters.
Syntax:
var s = string1 + string2
Example:
Output:
Hello People! My Name is Max.
To check whether the given strings are the same or not we use equal to(==), and not Equal to(!=) operators. Both operators are the most commonly used operator to compare two strings. Here both the operators return true if the given strings fulfill the condition. Otherwise, return false.
Syntax:
string1 == string2
or
string1 != string 2
Example:
Output:
hello is equal to hello hello is not equal to Krishna
To check if the given string is greater than another string we use the greater than operator(>). Here, > operator returns true if the first string is greater than another string. Otherwise, it will return false.
Syntax:
string1>string2
Example:
Output:
hen is greater than cow
To check if the given string is less than another string we use the less than operator(<). Here, < operator returns true if the first string is less than another string. Otherwise, it will return false.
Syntax:
string1<string2
Example:
Output:
Ant is less than cow
This function is used to check whether a given parameter string exists as a prefix of the string or not. If the string exists as a prefix in the other string, then it will return true. Otherwise, return false.
Syntax:
string1.hasPrefix(string2)
Example:
Syntax:
Does string1 starts with string2? true
This function is used to check whether a given parameter string exists as a suffix or not. If the string exists as a suffix in the other string, then it will return true. Otherwise, return false.
Syntax:
string1.hasSuffix(str2)
Example:
Output:
Does string1 ends with string2 : true
Lowercase means small letter alphabets. Here, we use this function for the conversion of all characters into lowercase characters in the given string.
Syntax:
string.lowercased()
Example:
Output:
Original String: HellO PeoPle Lowercase String: hello people
Uppercase means capital letter alphabets. Here, we use this function for the conversion of all characters into uppercase characters in the given string.
Syntax:
String.uppercased()
Example:
Output:
Original String : HellO PeOpLe Uppercase String : HELLO PEOPLE
Reversing a string means changing the order of a given string so that the last character of the string becomes the first character of the string and so on. This function is used for doing the reverse of the given string. With the help of this function, we can also check whether the string is 'Palindrome' or not. Palindrome means the sequence of characters which reads the same from backward as well as forwards, like, noon.
Syntax:
String. reversed()
Example:
Output:
Original String : Hello People Reversed String : elpoeP olleH
This function is used for inserting a character at a specified index in the string.
Syntax:
string1.insert(ch, at: I)
where 'ch' defines the character which has to be inserted and 'i' defines the index of the string at which 'ch' is to be inserted
Example:
Output:
Hellox People!
This function is used for removing characters at a specified index in the string.
Syntax:
string1.remove(at: I)
where 'i' is the index in the string from which the character will get removed.
Example:
Output:
Original string: Hello People! String after remove() : Hello eople! Character Removed : P