Kotlin Programming Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

Let’s quickly see a few other extension functions provided by Kotlin to parse String:

  • toBoolean(): Returns `true` if the content of this string is equal to the word true, ignoring case, and `false` otherwise.
  • toShort(): Parses the string as a [Short] number and returns the result. Also, it throws NumberFormatException if the string is not a valid representation of a number.
  • toShort(radix): Parses the string as a [Short] number and returns the result, throws NumberFormatException if the string is not a valid representation of a number, and throws IllegalArgumentException when [radix] is not a valid radix for the string to number conversion.
  • toInt(): Parses the string as an [Int] number and returns the result and throws NumberFormatException if the string is not a valid representation of a number.
  • toIntOrNull(): Parses the string as an [Int] number and returns the result or `null` if the string is not a valid representation of a number.
  • toIntOrNull(radix): Parses the string as an [Int] number and returns the result or `null` if the string is not a valid representation of a number, or @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
  • toFloat(): Parses the string as a [Float] number and returns the result, and @throws NumberFormatException if the string is not a valid representation of a number.
  • toDouble() : Parses the string as a [Double] number and returns the result, and @throws NumberFormatException if the string is not a valid representation of a number.