characters

How to create a range of characters as a Scala Array

I just noticed this quirk when trying to create an array of characters with the Scala Array.range method:

# works as expected
('a' to 'e').toArray              // Array[Char] = Array(a, b, c, d, e)

# surprise: Array.range always returns Array[Int]
val a = Array.range('a', 'e')     // Array[Int] = Array(97, 98, 99, 100)

I was surprised to see that the Scaladoc for the Array object states that the second example is expected behavior; Array.range always returns an Array[Int]. I suspect this has something to do with a Scala Array being backed by a Java array, but I didn’t dig into the source code to confirm this.

For much more information about arrays, see my Scala Array class examples tutorial.

Scala/Java: How to write a pattern that matches a minimum to maximum number of specified characters

If you’re using Java or Scala and need to write a pattern that matches a range of characters, where those characters occur between a minimum and maximum number of times in the pattern, the following example shows a solution I’m currently using.

The idea is that the pattern "[a-zA-Z0-9]{1,4}" means, “Match a string that has only the characters a-z, A-Z, and 0-9, where those characters occur a minimum of one time and a maximum of four times.” The following tests in the Scala REPL shows how this works:

Scala: Finding the difference, intersection, and distinct characters in a String

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 1.11, “String Differences, Intersections, and Distinct Characters.”

Problem

In Scala, you need to perform advanced string operations, such as finding the difference between two strings, the common characters between two strings, or the unique characters in a string.

How to process a Scala String one character at a time (with map, for, and foreach)

Scala FAQ: How can I iterate through each character in a Scala String, performing an operation on each character as I traverse the string?

Solution

Depending on your needs and preferences, you can use the Scala map or foreach methods, a for loop, or other approaches.

The map method

Here’s a simple example of how to create an uppercase string from an input string, using the map method that’s available on all Scala sequential collections:

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.

Scala: How to create a list of alpha or alphanumeric characters

While looking for code on how to create a random string in Scala, I came across this article, which shows one approach for creating a random string. For my brain today, it also shows a nice way to create a list of alpha or alphanumeric characters.

For instance, to create a list of alphanumeric characters, use this approach:

Scala: How to create a range of characters (list, sequence)

Scala range FAQ: How can I easily create a range of characters in Scala, such as a range of alpha or alphanumeric characters?

I learned recently that you can easily create a range of characters (a Range) as shown in the following examples. Here’s how you create a basic 'a' to 'z' range:

Java StringTokenizer - strings, words, and punctuation marks

I was just reading the book, Hadoop in Action, and came across a nice, simple way to use the Java StringTokenizer class to break a sentence (String) into words, taking into account many standard punctuation marks. Before looking at their solution, first take a look at the code they used to break a String into words using whitespace (a blank):

The Linux wc command (word count)

The Linux word count command is named wc. The wc command counts the number of characters, words, and lines that are contained in a text stream. If that sounds simple or boring, it's anything but; the wc command can be used in Linux command pipelines to do all sorts of interesting things.

Let's take a look at some Linux wc command examples to show the power of this terrific little command.

Java - strip unwanted characters from a string

Here's a quick line of Java code that takes a given input string, strips all the characters from that string other than lowercase and uppercase letters, and returns whatever is left: