Start learning free

LESSON 62 OF 414 · STRINGS I

String Find in Scala: indexOf and lastIndexOf

Lesson Read the full explanation

In Scala, you can find the index of a substring using indexOf():

s.indexOf(sub)           // returns index of first occurrence, or -1 if not found
s.lastIndexOf(sub)       // returns index of last occurrence
s.indexOf(sub, fromIdx)  // search starting from index `fromIdx`

Here are some examples:

var sentence = "Hello World"
var idx = sentence.indexOf("World")
println(idx)  // 6

println(sentence.indexOf("xyz"))  // -1  (not found)

The index returned is the position of the first character of the found substring.


Assignment
Follow the Coding Tutorial and find the index of "World" in sentence!


Hint
Use sentence.indexOf("World") and assign the result to idx.

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · STRINGS I

    String Find in Scala, explained

    Finding where a substring starts

    To locate text inside a string, call indexOf. It returns the index of the first character of the first match, counting from 0, or -1 when the text isn't there:

    val sentence = "Hello World"
    
    println(sentence.indexOf("World")) // 6
    println(sentence.indexOf("xyz"))   // -1
    

    "World" starts at the W, which is index 6 because Hello takes indexes 0 to 4 and the space is index 5. Like every Scala String method of this kind, indexOf comes from Java, so it works the same on the JVM.

    The variants

    • s.indexOf(sub) finds the first occurrence.
    • s.lastIndexOf(sub) finds the last occurrence.
    • s.indexOf(sub, from) starts searching at index from.
    • The argument can also be a single Char, such as s.indexOf('W').

    With a string that repeats a letter, the difference is easy to see:

    val s = "Hello World"
    
    println(s.indexOf("o"))     // 4
    println(s.indexOf("o", 5))  // 7
    println(s.lastIndexOf("o")) // 7
    

    The first o is at index 4. Starting the search at index 5 skips it and finds the second one at 7, which is also what lastIndexOf returns.

    The exercise, step by step

    The lesson gives you val sentence = "Hello World" and asks for the position of "World":

    1. Declare a new val named idx below the sentence.
    2. Assign it the result of sentence.indexOf("World"). The method returns an Int, so idx is an Int and holds 6.
    val sentence = "Hello World"
    
    val idx = sentence.indexOf("World")
    

    Gotchas

    • Case matters. "Hello World".indexOf("world") is -1. To ignore case, convert both sides first, for example sentence.toLowerCase.indexOf("world"), which gives 6.
    • Check for -1. -1 is a normal return value, not an error. If you use it as an index, as in s.substring(s.indexOf("x")), you get an exception instead of a clean "not found".
    • Only the start is returned. To get the matched text you need the length of the match as well, for example s.substring(i, i + sub.length).
    • Empty search text. "Hello".indexOf("") is 0, because the empty string matches at the very start.
    • Just a yes or no. If you only need to know whether the text occurs, s.contains("World") returns a Boolean and reads better than comparing with -1.

    Using the index

    An index from indexOf is a normal string index, so it combines with substring to cut text around a match:

    val space = sentence.indexOf(" ")
    
    println(sentence.substring(0, space))  // Hello
    println(sentence.substring(space + 1)) // World
    

    Finding every occurrence

    A single call finds only one match. To find all of them, use the from argument and restart just past each hit. In "Hello World", the letter l appears at indexes 2, 3 and 9. s.indexOf("l") gives 2, s.indexOf("l", 3) gives 3, and s.indexOf("l", 4) gives 9. A loop repeats this until the result is -1, which signals that no more matches remain. If from is past the end of the string, the answer is simply -1.

    Searching works on characters of any kind, including spaces, so s.indexOf(" ") is a quick way to find the end of a first word.

    Reading the result

    The return value carries two kinds of information. A number from 0 upward is a position. The special value -1 means "absent", and it can never be a real index, which is why Java chose it. A typical guard looks like if (idx != -1) ... else ..., or if (idx >= 0). For the lesson's sentence the guard passes, because 6 is a valid index.

    Key takeaways

    • indexOf returns the first matching index, or -1.
    • lastIndexOf searches for the last match, and the second argument of indexOf sets where to start.
    • Searches are case-sensitive.
    • Always handle the -1 result before using the index.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free