Printing the characters of a string value in Swift using the forEach
function or loop is easy. The forEach
function iterates over the string and repeats an action—in this case, it prints the character—for each element of the string.
string.forEach{
// print each character
print($0)
}
$0: This represents each character of the string as the loop executes.
// create a string let info = "Educative is the best!" // use forEach function // and print each character info.forEach{ // print each character print($0) }
forEach
function to print each character of the string.RELATED TAGS
CONTRIBUTOR
View all Courses