![]() |
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: By using String InstanceTo create a string we can use a String() initializer. It will create an instance of a string. Syntax:
Example: Swift
Output: Hello Everyone!!! By using String LiteralHere, 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:
Example: Swift
Output: Hello Everyone Welcome Everyone Multi-line StringsApart 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:
Example: Swift
Output: Hello People!!! This is an example of multiline string String Functions and OperatorsNow, we will see some commonly used functions and operators related to a string: Empty StringEmpty 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
isEmptySwift 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:
Example: Swift
Output: str1 is empty str2 is not empty String LengthThe 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:
Example: Swift
Output: Length of str is 25 String ConcatenationConcatenation 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:
Example: Swift
Output: Hello People! My Name is Max. String ComparisonTo 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:
Example: Swift
Output: hello is equal to hello hello is not equal to Krishna Greater than OperatorTo 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:
Example: Swift
Output: hen is greater than cow Less than OperatorTo 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:
Example: Swift
Output: Ant is less than cow hasPrefix(prefix: String)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:
Example: Swift
Syntax: Does string1 starts with string2? true hasSuffix(suffix: String)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:
Example: Swift
Output: Does string1 ends with string2 : true String.lowercased()Lowercase means small letter alphabets. Here, we use this function for the conversion of all characters into lowercase characters in the given string. Syntax:
Example: Swift
Output: Original String: HellO PeoPle Lowercase String: hello people String.uppercased()Uppercase means capital letter alphabets. Here, we use this function for the conversion of all characters into uppercase characters in the given string. Syntax:
Example: Swift
Output: Original String : HellO PeOpLe Uppercase String : HELLO PEOPLE String.reversed()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:
Example: Swift
Output: Original String : Hello People Reversed String : elpoeP olleH String.insert()This function is used for inserting a character at a specified index in the string. Syntax:
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: Swift
Output: Hellox People! String.remove(at:)This function is used for removing characters at a specified index in the string. Syntax:
where ‘i’ is the index in the string from which the character will get removed. Example: Swift
Output: Original string: Hello People! String after remove() : Hello eople! Character Removed : P |
Reffered: https://www.geeksforgeeks.org
Swift |
Related |
---|
![]() |
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |