Home > Enterprise >  Parse String to another appropriate type
Parse String to another appropriate type

Time:04-12

I receive String depends on case: "string" or "381" or "431.2" How can I parse it to String or Int or Double if possible?

CodePudding user response:

something like this would help:

fun checkContentType(): Any {
    val data: String = "12.3";
    val ir = data.toIntOrNull();
    if( ir != null ) return ir;
    val dr = data.toDoubleOrNull(); 
    if( dr != null ) return dr;
    return data;
}
  • Related