A Scala function to get the first paragraph from a string

As a brief note today, here’s the source code for a little Scala function that gets the first paragraph of text from a string. Paragraphs are assumed to be separated by \n\n characters. Also, if that character sequence isn’t found, the input text is assumed to be one paragraph. Here’s the source code:

def getFirstParagraph(text: String): String = {
    // get the index of the first `\n\n` and then get 
    // the text leading up to that position
    val paragraphSeparatorIdx = text.indexOf("\n\n")
    val subText = if (paragraphSeparatorIdx > 0) {
        text.substring(0, paragraphSeparatorIdx)
    } else {

As usual, the code can be shortened, but I write it out fully to make it easier to understand.

Feel free to use this code in your own projects, with the caveat being that I haven’t tested it yet. It looks correct and works on simple examples, but ...