How to convert String to Int in Kotlin?

前端 未结 7 1054
再見小時候
再見小時候 2020-12-15 02:30

I am working on a console application in Kotlin where I accept multiple arguments in main() function

fun main(args: Array) {
    /         


        
相关标签:
7条回答
  • 2020-12-15 02:49

    Actually, there are several ways:

    Given:

    var numberString : String = "numberString" 
    // number is the int value of numberString (if any)
    var defaultValue : Int    = defaultValue
    

    Then we have:

    +—————————————————————————————————————————————+——————————+———————————————————————+
    | numberString is a valid number ?            |  true    | false                 |
    +—————————————————————————————————————————————+——————————+———————————————————————+
    | numberString.toInt()                        |  number  | NumberFormatException |
    +—————————————————————————————————————————————+——————————+———————————————————————+
    | numberString.toIntOrNull()                  |  number  | null                  |
    +—————————————————————————————————————————————+——————————+———————————————————————+
    | numberString.toIntOrNull() ?: defaultValue  |  number  | defaultValue          |
    +—————————————————————————————————————————————+——————————+———————————————————————+
    
    0 讨论(0)
  • 2020-12-15 02:51
    val i = "42".toIntOrNull()
    

    Keep in mind that the result is nullable as the name suggests.

    0 讨论(0)
  • 2020-12-15 02:58
    fun getIntValueFromString(value : String): Int {
        var returnValue = ""
        value.forEach {
            val item = it.toString().toIntOrNull()
            if(item is Int){
                returnValue += item.toString()
            }
    
        }
        return returnValue.toInt()
    
    }
    
    0 讨论(0)
  • 2020-12-15 03:01

    I use this util function:

    fun safeInt(text: String, fallback: Int): Int {
        return text.toIntOrNull() ?: fallback
    }
    
    0 讨论(0)
  • 2020-12-15 03:08

    As suggested above, use 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.

    val a: Int? = "11".toIntOrNull()   // 11
    val b: Int? = "-11".toIntOrNull()  // -11
    val c: Int? = "11.7".toIntOrNull() // null
    val d: Int? = "11.0".toIntOrNull() // null
    val e: Int? = "abc".toIntOrNull()  // null
    val f: Int? = null?.toIntOrNull()  // null
    
    0 讨论(0)
  • 2020-12-15 03:12

    You could call toInt() on your String instances:

    fun main(args: Array<String>) {
        for (str in args) {
            try {
                val parsedInt = str.toInt()
                println("The parsed int is $parsedInt")
            } catch (nfe: NumberFormatException) {
                // not a valid int
            }
        }
    }
    

    Or toIntOrNull() as an alternative:

    for (str in args) {
        val parsedInt = str.toIntOrNull()
        if (parsedInt != null) {
            println("The parsed int is $parsedInt")
        } else {
            // not a valid int
        }
    }
    

    If you don't care about the invalid values, then you could combine toIntOrNull() with the safe call operator and a scope function, for example:

    for (str in args) {
        str.toIntOrNull()?.let {
            println("The parsed int is $it")
        }
    }
    
    0 讨论(0)
提交回复
热议问题