Working with Strings in SwiftUI - Tips and Tricks
In any programming language, a string is a data type and is basically a series of characters. Let's learn about Strings in Swift.
Learn the basics of Strings in Swift and how to manipulate them.
The basics
A String is defined by double quotation marks:
let myString = "Hello, world"
..
If you want to add a multilines string, you can do so with three double quotation marks:
let myMultilineString = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
..
Concatenating Strings
If you want to join two strings together, just use the + operator.
let myNewString = "This is my" + "concatenated string."
// Output: "This is my concatenated string."
..
String Interpolation
To interpolate a variable inside of a string, just put the variable in ().
let age = 25
let myStringInterpolation = "I am \(age) years old."
// Output: "I am 25 years old."
..
String Includes
To check if a string includes a certain character or another string, use the .contains method:
let myString = "The quick brown fox jumps over the lazy dog"
myString.contains("brown fox")
// Output: true
..
Lowercase and Uppercase String
To lowercase everything in your String, simply call the .lowercased() method.
let myString = "Monday, Tuesday, Wednesday"
myString.lowercased()
// Output: "monday, tuesday, wednesday"
..
To uppercase everything in your String, simply call the .uppercased() method.
let myString = "Monday, Tuesday, Wednesday"
myString.uppercased()
// Output: "MONDAY, TUESDAY, WEDNESDAY"
..
Comments
Post a Comment