String is a fundamental value type in JavaFX. Similar to Java and other languages on the JVM, the String type is used to represent text literals within a single or double quotes. Unlike Java, however, JavaFX strings have additional capabilities which will be explored in this section.
We have already seen how to use String types in other recipes. When creating a String, you simply create a literal or expression to represent the string's content, and use the curly braces to embed expressions as shown below. The full listing for this code can be found in ch01/source-code/src/javafx/StringDemo.fx
.
var str1:String = "Hello World!"; var str2 = "Goodbye forever"; var title = "King"; println ("The {title} has arrived!"); var evens = [0, 2, 4, 6, 8]; println("What are the odds {for(d in evens) "{d + 1} "}"); var amount = 445234.66; println ("Your house is worth ${%,.2f amount}");
Similar to other types, a string can be declared with a literal representation, participate in expressions, and hold a value. The previous snippet shows the literal declaration of a string. Variable str2
is coerced by the type-inference engine into a String type implicitly.
One of the interesting features of the String type in JavaFX is its ability to have embedded expressions (similar to other templating languages) enclosed in curly braces. In the previous code snippet, println ("The {title} has arrived!")
will print the string with the value of the variable title
embedded in it.
You can also have complex expressions embedded in the string, as is shown from the code snippet println("What are the odds {for(d in evens) "{d + 1} "}")
from the recipe. The embedded expression contains a loop that traverses elements from variable evens
and outputs the result from the nested string "{d + 1}
" with each pass, producing new string What are the odds 1 3 5 7 9
.
The JavaFX Sting type has the ability to process string formatting expressions based on Java's java.util.Formatter
class. In the previous code snippet, we used format expression %,.2f
to format the variable amount
which displays Your house is worth $445,234.66
. You can find information about supported format expressions at http://java.sun.com/javase/6/docs/api/java/util/Formatter.html.
Before we leave the discussion on String, it's worth taking a look at localization. In JavaFX, the localization mechanism is an extension of the string expression.
To mark a String literal as a localized string, simply prefix the string with double hashes. This causes JavaFX to substitute the string with a localized string, if one is found, from a locale properties file.
To illustrate, let's look at an example. The code for this example is found in package ch01/source-code/src/locale
.
Create a JavaFX script file with the following content:
var msg1 = ##"Lift the cover"; var msg2 = ##[red button]"Press the red button to destroy"; println (msg1); println (msg2);
Save the file as Localization.fx
and compile. Now, create a text file named Localization_fr.fxproperties
and type in the following:
"Lift the cover"="Soulevez le couvercle" "red button"="Appuyez sur le bouton rouge pour détruire"
Notice that JavaFX can use either the actual string or a string key (red button
) to do the substitution for the localized string. When the code is compiled and executed, the output is:
Soulevez le couvercle Appuyez sur le bouton rouge pour détruire
The strings are substituted automatically by the JavaFX runtime with their French translation. If no properties file is found for the locale, JavaFX defaults to the actual String assigned to the variable.