In this article you will examine various techniques to find out whether a string is numeric or not in Kotlin. Method 1: Check if a string is numeric or not using regular expressions (regex) fun main(args: Array<String>) { val string = “-1234.15” var numeric = true numeric = string.matches(“-?\\d+(\\.\\d+)?”.toRegex()) if (numeric) println(“$string is a number”) ..
Category : Kotlin Interview Q&A
In this article you will learn today how to sort an array list of custom objects by the property given in Kotlin. Sort ArrayList of Custom Objects By Property import java.util.* fun main(args: Array<String>) { val list = ArrayList<CustomObject>() list.add(CustomObject(“Z”)) list.add(CustomObject(“A”)) list.add(CustomObject(“B”)) list.add(CustomObject(“X”)) list.add(CustomObject(“Aa”)) var sortedList = list.sortedWith(compareBy({ it.customProperty })) for (obj in sortedList) { ..
In this article you will learn today how to sort a given map in Kotlin. Sort a map by values fun main(args: Array<String>) { var capitals = hashMapOf<String, String>() capitals.put(“Nepal”, “Kathmandu”) capitals.put(“India”, “New Delhi”) capitals.put(“United States”, “Washington”) capitals.put(“England”, “London”) capitals.put(“Australia”, “Canberra”) val result = capitals.toList().sortedBy { (_, value) -> value}.toMap() for (entry in result) { ..
In this article you will learn today how to compare two wires in Kotlin. Method 1: Compare two strings using equals() fun main(args: Array<String>) { val style = “Bold” val style2 = “Bold” if (style.equals(style2)) println(“Equal”) else println(“Not Equal”) } Result Equal Method 2 : Compare two strings fun main(args: Array<String>) { val style = ..
In this article you will learn today how to convert string values to enum using the valueOf () method of an enum in Komalin. Lookup enum by string value enum class TextStyle { BOLD, ITALICS, UNDERLINE, STRIKETHROUGH } fun main(args: Array<String>) { val style = “Bold” val textStyle = TextStyle.valueOf(style.toUpperCase()) println(textStyle) } R..
In this article you will learn today how to convert output to string using string initializer in Kotlin. Convert OutputStream to String import java.io.* fun main(args: Array<String>) { val stream = ByteArrayOutputStream() val line = “Hello there!” stream.write(line.toByteArray()) val finalString = String(stream.toByteArray()) println(finalString) } Result Hello the..
In today’s article you will learn how to convert inputstream to string in Kotlin program? Convert InputStream to String import java.io.* fun main(args: Array<String>) { val stream = ByteArrayInputStream(“Hello there!”.toByteArray()) val sb = StringBuilder() var line: String? val br = BufferedReader(InputStreamReader(stream)) line = br.readLine() while (line != null) { sb.append(line) line = br.readLine() } br.close() ..
Today we will read on this page how to convert file object in byte [] and vice versa in Kotlin. Before we convert a file into a byte array and vice versa, let’s assume that we have a file named test.txt in our src folder. Here is the content of test.txt This is a Test ..
Today we will read on this page how to convert stack trace into string in Kotlin. Convert stack trace to a string import java.io.PrintWriter import java.io.StringWriter fun main(args: Array<String>) { try { val division = 0 / 0 } catch (e: ArithmeticException) { val sw = StringWriter() e.printStackTrace(PrintWriter(sw)) val exceptionAsString = sw.toString() println(exceptionAsString) } } ..
Today we will read various techniques to add text to an existing file in Kotlin on this page. Before creating a string from a file, we assume that we have a file named test.txt in our src folder. Content of test.txt This is a Test file. Method 1: Append text to an existing file using ..