@ -5,6 +5,7 @@ The Spring Expression Language supports the following kinds of operators:
@@ -5,6 +5,7 @@ The Spring Expression Language supports the following kinds of operators:
* xref:core/expressions/language-ref/operators.adoc#expressions-operators-relational[Relational Operators]
* xref:core/expressions/language-ref/operators.adoc#expressions-operators-logical[Logical Operators]
* xref:core/expressions/language-ref/operators.adoc#expressions-operators-string[String Operators]
* xref:core/expressions/language-ref/operators.adoc#expressions-operators-mathematical[Mathematical Operators]
* xref:core/expressions/language-ref/operators.adoc#expressions-assignment[The Assignment Operator]
@ -207,14 +208,80 @@ Kotlin::
@@ -207,14 +208,80 @@ Kotlin::
======
[[expressions-operators-string]]
== String Operators
You can use the following operators on strings.
* concatenation (`+`)
* subtraction (`-`)
- for use with a string containing a single character
* repeat (`*`)
The following example shows the `String` operators in use:
[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
// -- Concatenation --
// evaluates to "hello world"
String helloWorld = parser.parseExpression("'hello' + ' ' + 'world'")
.getValue(String.class);
// -- Character Subtraction --
// evaluates to 'a'
char ch = parser.parseExpression("'d' - 3")
.getValue(char.class);
// -- Repeat --
// evaluates to "abcabc"
String repeated = parser.parseExpression("'abc' * 2")
.getValue(String.class);
----
Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
----
// -- Concatenation --
// evaluates to "hello world"
val helloWorld = parser.parseExpression("'hello' + ' ' + 'world'")
.getValue(String::class.java)
// -- Character Subtraction --
// evaluates to 'a'
val ch = parser.parseExpression("'d' - 3")
.getValue(Character::class.java);
// -- Repeat --
// evaluates to "abcabc"
val repeated = parser.parseExpression("'abc' * 2")
.getValue(String::class.java);
----
======
[[expressions-operators-mathematical]]
== Mathematical Operators
You can use the addition operator (`+`) on both numbers and strings. You can use the
subtraction (`-`), multiplication (`*`), and division (`/`) operators only on numbers.
You can also use the modulus (`%`) and exponential power (`^`) operators on numbers.
Standard operator precedence is enforced. The following example shows the mathematical
operators in use:
You can use the following operators on numbers, and standard operator precedence is enforced.
* addition (`+`)
* subtraction (`-`)
* multiplication (`*`)
* division (`/`)
* modulus (`%`)
* exponential power (`^`)
The following example shows the mathematical operators in use:
[tabs]
======
@ -225,9 +292,6 @@ Java::
@@ -225,9 +292,6 @@ Java::
// Addition
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
String testString = parser.parseExpression(
"'test' + ' ' + 'string'").getValue(String.class); // 'test string'
// Subtraction
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
@ -259,9 +323,6 @@ Kotlin::
@@ -259,9 +323,6 @@ Kotlin::
// Addition
val two = parser.parseExpression("1 + 1").getValue(Int::class.java) // 2
val testString = parser.parseExpression(
"'test' + ' ' + 'string'").getValue(String::class.java) // 'test string'
// Subtraction
val four = parser.parseExpression("1 - -3").getValue(Int::class.java) // 4