Christopher Mederos
06/26/2024, 5:01 AM/**
* Returns a string representation of the object. Can be called with a null receiver, in which case
* it returns the string "null".
*/
public fun Any?.toString(): String
Is there a version of Any?.toString() that would return an empty string instead of the "null" when called with a null neceiver?Christopher Mederos
06/26/2024, 5:04 AMmyOptional?.toString() ?: ""
Szymon Jeziorski
06/26/2024, 5:53 AMString?.orEmpty()
extension, which would result in exactly the same. Example:
myOptional?.toString().orEmpty()
Christopher Mederos
06/26/2024, 6:27 AMCLOVIS
06/26/2024, 7:23 AMfun Any?.toStringOrEmpty() =
this?.toString().orEmpty()
Klitos Kyriacou
06/26/2024, 8:30 AMtoString()
is called implicitly, such as println(x)
.