Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Iterating in Strings | Strings
Introduction to Scala
course content

Contenido del Curso

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

bookIterating in Strings

Iterating over the Elements

First of all, as we have mentioned in the previous chapter, a string in Scala is an array of bytes where each byte is converted to Char. What this means is that a string can be thought of as a collection of chars, so we can iterate over its elements the same way as we did in arrays:

java

Main

copy
12345678
object Main { def main(args: Array[String]): Unit = { val greeting = "Hello World!" for (character <- greeting) { print(character) // Prints every character one after another on the same line } } }

Basically, we did exactly what the print(greeting) function would do.

With this in mind, let's now print the next characters in the ASCII table for each of the letters in our string while printing ' ' and '!' as they are:

java

Main

copy
12345678910111213
object Main { def main(args: Array[String]): Unit = { val greeting = "Hello World!" for (character <- greeting) { if ((character != ' ') && (character != '!')) { print((character + 1).toChar) // Prints the next character in the ASCII table for each of the letters } else { print(character) } } } }

Once again, the character variable is a val, so we cannot modify it anyhow.

Iterating over the Indices Using .length

Similarly to arrays, strings also have the length method (not property like in arrays) which returns the number of characters in the string. Using this method with until or to, we can iterate over the indices of the string. This approach is useful to iterate over specific indices.

Let's take a look at the following example:

java

Main

copy
12345678
object Main { def main(args: Array[String]): Unit = { val greeting = "(Hello World!)" for (i <- 1 until greeting.length - 1) { print(greeting(i)) // Prints every character except for the parentheses } } }

As you can see, there is essentially nothing new here.

Before we dive into a more practical example, however, let's first understand what cryptography is.

Now, suppose we have a certain secret message which we want to decode. It was encrypted in the following way: every character of the original message was replaced with the character which has the ASCII encoding equal to to the encoding of the original character + 3. Moreover, ( was added to the left of the message, and ) was added to the right.

Let's now get and print the original message:

java

Main

copy
12345678910
object Main { def main(args: Array[String]): Unit = { val encryptedMessage = "(Khoor#Zruog$)" var originalMessage = "" for (i <- 1 until encryptedMessage.length - 1) { originalMessage += (encryptedMessage(i) - 3).toChar // String concatenation } print(originalMessage) } }

Iterating over the Indices Using .indices

Once again, similarly to arrays, we can iterate over the indices of the string using its indices property. When we iterate over all indices of the string this approach is preferred to using .length with to or until.

Let's take a look at a simple example:

java

Main

copy
12345678
object Main { def main(args: Array[String]): Unit = { val greeting = "Hello friends!" for (i <- greeting.indices) { println(greeting(i)) } } }

As you can see, there is absolutely nothing complicated here.

What will be printed?

What will be printed?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 2
some-alt