string

Scala - How to count the number of occurrences of a character in a String

Scala String FAQ: How can I count the number of times (occurrences) a character appears in a String?

Use the count method on the string, using a simple anonymous function, as shown in this example in the REPL:

scala> "hello world".count(_ == 'o')
res0: Int = 2

There are other ways to count the occurrences of a character in a string, but that's very simple and easy to read.

A Scala 2.10 implicit class example

Scala FAQ: Can you share an example of how to create an implicit class in Scala 2.10?

Sure. As your question implies, the implicit class functionality changed in Scala 2.10, so let's take a look at the new syntax.

Convert a Scala array to string with mkString

Scala collections FAQ: How can I convert a Scala array to a String?

A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)

Here's a quick array to string example using the Scala REPL:

String interpolation in Scala 2.10 (embedding variables in strings)

Scala string FAQ: How do I embed variables in strings in Scala?

There are a lot of nice new features in Scala 2.10, but a simple feature that makes me happy is string interpolation. In short, Scala developers can now embed variables inside strings just like you can do in other languages like Perl, PHP, and Ruby -- but perhaps in an even more powerful way. (I say "perhaps" because I haven't used those other languages recently.)

Different ways to create random strings in Scala

When it comes to generating random strings with the scala.util.Random class, I haven't figured out yet how to use the nextString method. I've tried using it in several different ways, but I always get a string of question marks as output, like this:

A Scala String chomp (or chop) function

Scala String FAQ: Does Scala have a String method like chomp or chop that you find in Perl?

If you're using Scala and want a String chomp or chop method you're used to in languages like Perl to remove end of line characters, it looks like the stripLineEnd method will do what you want, as shown in the REPL:

Scala - matching a regex pattern against an entire String

Scala regex FAQ: How can I match a regex pattern to an entire string in Scala?

This morning I needed to write a little Scala code to make sure a String completely matches a regex pattern. I started off by creating a Scala Regex instance, and then realized the Regex class doesn't have a simple method to determine whether a String completely matches a pattern.

Java String comparison FAQ: How to compare Java Strings

Java String comparison FAQ: Can you share some examples of how to compare Strings in Java?

If you're like me, when I first started using Java, I wanted to use the "==" operator to test whether two String instances were equal, but for better or worse, that's not the correct way to do it in Java.

Does Scala have a String variable substitution syntax like Ruby?

Scala FAQ: Does Scala have a String variable substitution syntax like Ruby?

UPDATE: If you're using Scala 2.10 or newer, see my new String interpolation in Scala 2.10 (embedding variables in strings) tutorial. If you're using Scala 2.9.x or older, continue with this article.

Iterating over Scala Maps (for and foreach loop examples)

Scala Map FAQ: How can I iterate/loop over a Scala Map?

There are several different ways to iterate over a Scala Map, and the method you choose depends on the problem you need to solve.

To get started with our examples, let's create a simple Map we can work with:

Syndicate content