Library contents

Scala String.linesIterator

Count logical lines in a saved note, including a blank line between two entries.

A simple example

String.linesIterator

  1. Start with Two lines containing A then B
  2. Do this Read each line
  3. Result 2 lines
The newline separates the two records. The letters are the contents of those records.

Syntax

Scala syntax
value.linesIterator.size
Run these examples in Scala

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

Count handover tasks

A handover or shopping note stores one entry per line. The display needs a count of the entries after interpreting line boundaries rather than counting the entire note as one item.

From the programval value = "Open\nCheck\nClose"
Result3
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "Open\nCheck\nClose"
        println(value.linesIterator.size)
    }
}
Output
3

Each task occupies one line.

Advertisements

Preserve a blank row for an unsigned delivery

A line-based import may include a blank record or a last record without a terminating newline. Distinguish a legitimate line from the end of the input so the import keeps the right number of rows.

From the programval value = "Arrived\n\nSigned"
Result3
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "Arrived\n\nSigned"
        println(value.linesIterator.size)
    }
}
Output
3

The interior blank line still occupies its own row.

Read a studio checklist

A stage manager places each job on its own line. Count the lines of work in the saved note. The final line has no trailing newline.

GivenOpen / Test / Close
Worked result3

What to watch for

A final newline convention and the recognized line endings vary by API. These examples use LF text without a final newline.

Documentation for String.linesIterator