


The substring “Green eggs and ham” is found at position 91. Println("Index of 'Green eggs and ham' => " + paragraph.indexOf("Green eggs and ham")) If we want to find where a certain substring begins, we use the indexOf(String) method. This method also has an optional argument that allows it to ignore case. Since computers count starting at 0, the result is 2. The letter ‘a’ is the 3rd character in the example string. Println("Index of letter a => " + paragraph.indexOf('a')) It will return the index where the character was first found or -1 if the string doesn’t contain such a character. The indexOf(Char) method is used to search for a single character within a string. Let’s go over the indexOf() method with a few examples. Such an operation is especially useful in situations where you may need to break a string into a substring or divide a string into different parts. Here’s the link to the cheatsheet covering all 5 parts again.The Kotlin String class has an indexOf() method that allows developers to the position of a character or set of characters within a string. That’s it for part 4! The 5th and final Kotlin for Interviews article covers Frequently Used Code Snippets.

The one you’ll encounter the most often is IntRange, but you can use LongRange and CharRange as well.

The step, or distance between two values, has a default value of 1. Refresher on RangeĪ Range in Kotlin is a sequence of values defined by a start value, an end value, and a step. Many interview problems require some kind of iteration, be it manipulating an input array or using a map to store information, so I’ll go over different ways of iterating over some common data structures. You can find Part 1: Common Data Types here, Part 2: Collection Functions here and Part 3: Numbers and Math here and Part 5: Frequently Used Code Snippets here. also compiled a cheatsheet that covers all 5 parts of this series, which you can find here. This is Part 4 of Kotlin for Interviews, a series where I go over Kotlin functions and code snippets that came up often during my Android interview prep.
