- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert String to Long in Kotlin?
In this article, we will see how to convert a String to Long in Kotlin using a library function. There are multiple ways to do it. Let's take a couple of examples to demonstrate how it's done.
Example - using toLong()
toLong() is a function that provides the most convenient way to convert a string to a long. In the following example, we will see how we can use toLong() to convert our String.
fun convertToLong(s: String) { try { val value = s.toLong() println("The Long value is: $value") } catch (ex: NumberFormatException) { println("Please enter a number: ") } } fun main() { val str = "1234567890" convertToLong(str) }
Output
Once the above piece of code is executed, it will convert our String "1234567890" to a Long value.
The Long value is: 1234567890
Example - using toLongOrNull()
Like toLong(), we can use another function called toLongOrNull() to convert a String value to Long. In the following example, we will see how to convert a String to Long using toLongOrNull().
fun convertToLong(s: String) { try { val value = s.toLongOrNull() println("The Long value is: $value") } catch (ex: NumberFormatException) { println("Please enter a number: ") } } fun main() { val str = "1234567890" convertToLong(str) }
Output
Once the above piece of code is executed, it will convert our String "1234567890" to a Long value.
The Long value is: 1234567890
Example - java.lang.Long.valueOf()
Kotlin is based on the JVM. Hence, we can use the Java Lang package to convert a String to a Long variable. In the following example, we have used the valueOf() function to convert a String to a Long variable.
fun main() { val str = "12345678" println("The given string is: " +str) val value = java.lang.Long.valueOf(str) println("After converting to Long: " +value) }
Output
One executed, the above piece of code will convert our String "1234567890" to a Long value.
The given string is: 12345678 After converting to Long: 12345678
- Related Articles
- How to convert Int to Hex String in Kotlin?
- How to convert an ArrayList to String in Kotlin?
- Convert String to Long in Java
- Convert from String to long in Java
- C# program to convert string to long
- How to convert an image into base64 String in Android using Kotlin?
- How to convert ISO 8601 String to Date/Time object in Android using Kotlin?
- How to convert Long array list to long array in Java?
- How to convert a Bitmap to Drawable in Kotlin?
- How to convert TimeStamp to DateTime in Kotlin?\n
- How to convert a Base64 string into a BitMap image in Android App using Kotlin?
- Convert long primitive to Long object in Java
- How to convert a List to a Map in Kotlin?
- Convert Long into String using toString() method of Long class in java
- How to convert milliseconds to date format in Android using Kotlin?
