I have the following function which reverses a string value's display order. I'm new to Swift and I'm trying to understand it's logic. What is going on with the '!pleh' value that it turns into 'Help!' ? Thanks
func reverse(_ s: String) -> String {
var str = ""
for character in s.characters {
str = "\(character)" + str
}
return str
}
print (reverse("!pleH"))
The REVERSE function accepts a character expression as its argument, and returns a string of the same length, but with the ordinal positions of every logical character reversed.
To reverse a string in java, we can first convert it to StringBuilder which is nothing but a mutable string. Class StringBuilder provides an inbuilt function called reverse(), which reverses the given string and provides you with the final output.
With a string as an argument, reversed() returns an iterator that yields characters from the input string in reverse order.
In swift 4.0, directly call reversed
on a string will get the job done
let str = "abc"
String(str.reversed()) // This will give you cba
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With