Kotlin String Parsing

Kotlin is a strongly typed language, which means that variables of one type may not be used for another type. In many cases, strong typing is a benefit. For example, since the language only allows Int variables to hold int values, we have some protection (not complete) against injection attacks that attempt to insert a string of code into an int variable.

On a more practical manner, strong typing helps us prevent bugs. The compiler can check ahead of time if a variable has certain attributes or methods prior to runtime. We are also protected against unsafe implicit conversions of data, which is a common problem in weakly typed languages. However, strong typing has a drawback. We are forced to convert between data types.

In languages such as Java, this can be really troubling. Consider the following Java code snippet.

String three = "3";
int t = Integer.parseInt(three);

String four = String.valueOf(4);

Java’s approach to converting between Strings and Ints is clunky, to say the least. Since Java primitives are not objects, wrapper classes have to be used to facilitate conversion between types when casting won’t work. Using wrapper classes add verbosity to the code and mixes a procedural approach in with what could be an OOP approach.

Oddly, this is true of Java’s String class also. Strings are objects in Java, yet you will not find methods such as toInt() or toDouble() on the String class. Once again, you are forced to turn to a primitive type wrapper class methods such as Integer.parseInt(). Using such an approach feels unnatural, to say the least.

Kotlin Conversions

Kotlin treats all variables as objects. Furthermore, variables have conversion methods. So if when we want to convert a String to an Int in Kotlin, we call toInt() on the String. When we want to convert an Int to a String, we call toString(). The approach is much more OOP in design since we are calling the behavior on the object that we are using.

Here are a few code examples to demonstrate type conversions in Kotlin.

//convert "3" to 3
val three = "3".toInt()

//convert 3 to "3"
val threeStr = three.toString()

//convert "3.14159" to a double
val pi = "3.14159".toDouble()

It should be noted that while type conversions in Kotlin may be more simple than Java, they aren’t safer. If we attempt to call toInt() on a string that isn’t a number, we will get an exception.

val three = "Mr. Pickles".toInt() //Bad!!!

The runtime isn’t able to figure out what number is represented by the string “Mr. Pickles” as such, we will get a runtime exception. If we aren’t sure if a conversion is safe, we will need to wrap the cast in a try-catch block or turn to a third party library such as StringUtils in Apache Commons.

Putting it Together

Below is an example program that demonstrates String parsing and type conversions.

fun main(args : Array<String>){
    val three = 3
    val pi = 3.14159
    val t = true

    //Note: We are using toString() explicitly here for demonstration
    println("Converting 3 to String => " + three.toString())
    println("Converting pi to String => " + pi.toString())
    println("Converting t to String => " + t.toString())

    val threeNum = "3".toInt()
    val piNum = "3.14159".toDouble()
    val tBool = "true".toBoolean()

    println("threeNum => " + threeNum)
    println("piNum => " + piNum)
    println("tBool => " + tBool)


Converting 3 to String => 3
Converting pi to String => 3.14159
Converting t to String => true
threeNum => 3
piNum => 3.14159
tBool => true

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s