Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
Swift offers a rich set of functions and operators to work with strings. Below, we'll cover some of the most commonly used string functions and operators:
You can use the +
operator to concatenate two strings:
let hello = "Hello, " let world = "world!" let greeting = hello + world print(greeting) // Outputs: "Hello, world!"
Swift supports string interpolation using the \(value)
syntax:
let name = "John" let age = 30 let introduction = "My name is \(name) and I am \(age) years old." print(introduction) // Outputs: "My name is John and I am 30 years old."
Use the isEmpty
property:
let emptyString = "" if emptyString.isEmpty { print("The string is empty!") }
You can get the number of characters in a string with the count
property:
let text = "Hello" print(text.count) // Outputs: 5
You can use indices to access specific characters or ranges in a string:
let word = "Swift" let firstLetter = word[word.startIndex] print(firstLetter) // Outputs: "S"
You can use the range(of:)
method:
let sentence = "Swift is awesome!" if let range = sentence.range(of: "awesome") { print("Found the word 'awesome'!") }
Use the replacingOccurrences(of:with:)
method:
let original = "I love programming." let replaced = original.replacingOccurrences(of: "programming", with: "Swift") print(replaced) // Outputs: "I love Swift."
let content = "Swift Rules" print(content.uppercased()) // Outputs: "SWIFT RULES" print(content.lowercased()) // Outputs: "swift rules"
You can use the split(separator:)
method:
let fruits = "apple,orange,banana" let fruitArray = fruits.split(separator: ",") print(fruitArray) // Outputs: ["apple", "orange", "banana"]
let description = "Swift programming" if description.hasPrefix("Swift") { print("Starts with 'Swift'") } if description.hasSuffix("programming") { print("Ends with 'programming'") }
This list is by no means exhaustive; the Swift standard library contains many other methods and properties for string manipulation. But these are some of the basic and commonly used functions and operators.
Concatenation and interpolation in Swift strings:
Description: Concatenation involves combining strings using the +
operator, while interpolation allows inserting variables into strings.
Code:
let firstName = "John" let lastName = "Doe" // Concatenation let fullName = firstName + " " + lastName // Interpolation let interpolatedFullName = "\(firstName) \(lastName)"
Swift string comparison operators:
Description: String comparison in Swift can be done using equality (==
) and inequality (!=
) operators.
Code:
let str1 = "apple" let str2 = "orange" if str1 == str2 { print("Strings are equal") } else { print("Strings are not equal") }
Substring extraction in Swift:
Description: Extracting substrings in Swift is done using the prefix
, suffix
, and dropFirst
/ dropLast
methods.
Code:
let originalString = "Swift Programming" // Extracting substring let prefixSubstring = originalString.prefix(5) // "Swift" let suffixSubstring = originalString.suffix(11) // "Programming" let droppedSubstring = originalString.dropFirst(6) // "Programming"
Searching and replacing in Swift strings:
Description: Swift provides methods for searching and replacing substrings within a string.
Code:
let sentence = "Swift is a powerful language." // Searching if let range = sentence.range(of: "powerful") { print("Found at index \(sentence.distance(from: sentence.startIndex, to: range.lowerBound))") } // Replacing let newSentence = sentence.replacingOccurrences(of: "powerful", with: "amazing")
String length and count functions in Swift:
Description: The length of a string in Swift can be obtained using the count
property or the count
method.
Code:
let message = "Hello, World!" // Using count property let length1 = message.count // Using count method let length2 = message.count
Formatting strings in Swift:
Description: String formatting in Swift can be achieved using the String(format:)
constructor and format specifiers.
Code:
let price = 29.99 // Formatting with currency symbol let formattedPrice = String(format: "$%.2f", price) // "$29.99"
Using Swift string methods and operators:
Description: Swift provides a variety of string methods and operators for manipulation, such as lowercased()
, uppercased()
, isEmpty
, and others.
Code:
let text = "Swift Programming" // Convert to lowercase let lowercaseText = text.lowercased() // Check if the string is empty let isEmpty = text.isEmpty