About Swift Syntax 3 (Frequently used functions on Integer, String)
About “Swift Syntax 3 (Frequently used functions on Integer, String)”
I had the opportunity to study iOS this week, and I’ve been working on grammar, so I thought I’d write a post to summarize it.
In this post, I’ll cover min and max for Integer and various functions for String.
The Integer type
The min and max functions
Global functions to find the smaller or larger of two integers.
Example
let a = 10
let b = 3
let minimum = min(a, b) // 3
let maximum = max(a, b) // 10
Here are some of the most commonly used functions of the String type.
String type
contains function
Checks if a string contains a specific character or substring.
Example
let string = "Hello, Swift!"
if string.contains("Swift") {
print("Contains Swift!")
}
// Output: Contains Swift!
hasPrefix function
Checks if a string starts with a specific prefix.
Example
if string.hasPrefix("Starts") {
print("Starts with Hello!")
}
// output: Starts with Hello!
hasSuffix function
Checks if a string ends with a specific suffix.
Example
if string.hasSuffix("!") {
print("Ends with an exclamation mark!")
}
// Output: Ends with an exclamation mark!
lowercased function
Converts a string to all lowercase letters.
Example
let lowercasedString = string.lowercased()
print(lowercasedString)
// Output: hello, swift!
uppercased function
Converts a string to all uppercase letters.
Example
let uppercasedString = string.uppercased()
print(uppercasedString)
// Output: HELLO, SWIFT!
count function
Returns the length of a string.
Example
let length = string.count
print(length)
// Output: 13
replacingOccurences function
Replaces specific occurrences of a string with another string.
Example
let replacedString = string.replacingOccurrences(of: "Swift", with: "World")
print(replacedString)
// Output: Hello, World!