Fandom

Planet JFX

String Concatenation

118pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Unlike Java, JavaFX does not overload the + operator to concatenate Strings:

import javafx.ui.*;
import javafx.ui.canvas.*;
	
Frame {
    content: Label {
        text: "Hello " + "World"
    }
    visible: true
}

Attempting to run the code above will result in the following console output:

compile thread: Thread[AWT-EventQueue-0,6,main]
compile 2.031
file:/C:/workspace/F3/HelloWorld.fx:6: incompatible types: expected Number, found String in "Hello "
file:/C:/workspace/F3/HelloWorld.fx:6: incompatible types: expected Number, found String in "World"
file:/C:/workspace/F3/HelloWorld.fx:6: incompatible types: expected String, found Number in text: "Hello " + "World"

Interpolation Work-AroundEdit

Interpolation, using the JavaFX String expression operators { }, provides a very readable work-around; the equivalent of Java's

String s = "Your score is " + n + " out of " + total + ".";

is this in JavaFX:

var s:String = "Your score is {n} out of {total}.";

concat() Method Work-AroundEdit

The concat() method may also be used in JavaFX to concatenate two Strings:

import javafx.ui.*;
import javafx.ui.canvas.*;
	
Frame {
    content: Label {
        text: "Hello ".concat("World")
    }
    visible: true
}

Also on Fandom

Random Wiki