ifNotBlank

inline fun <R> String?.ifNotBlank(func: (String) -> R): R?

Executes a function on a string if it is not null, empty, or blank (contains only whitespace). This is a null-safe utility function that provides a concise way to perform operations on non-blank strings while returning null for blank inputs.

Return

the result of the function if the string is not blank, null otherwise

Example usage:

val result = "  hello  ".ifNotBlank { it.trim().uppercase() } // returns "HELLO"
val nullResult = " ".ifNotBlank { it.trim() } // returns null
val nullInput = null.ifNotBlank { it.length } // returns null

Parameters

R

the return type of the function

func

the function to execute if the string is not blank