Skip to content

Commit

Permalink
Document repeat and characer subtraction String operators in SpEL
Browse files Browse the repository at this point in the history
Closes gh-32137
  • Loading branch information
sbrannen committed Jan 26, 2024
1 parent fdf0a6f commit b9bad56
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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]
======
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -283,14 +283,26 @@ void logicalOperators() {
}

@Test
void numericalOperators() {
void stringOperators() {
// Concatenation
String helloWorld = parser.parseExpression("'hello' + ' ' + 'world'").getValue(String.class);
assertThat(helloWorld).isEqualTo("hello world");

// Character Subtraction
char ch = parser.parseExpression("'d' - 3").getValue(char.class);
assertThat(ch).isEqualTo('a');

// Repeat
String repeated = parser.parseExpression("'abc' * 2").getValue(String.class);
assertThat(repeated).isEqualTo("abcabc");
}

@Test
void mathematicalOperators() {
// Addition
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
assertThat(two).isEqualTo(2);

String testString = parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class); // 'test string'
assertThat(testString).isEqualTo("test string");

// Subtraction
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
assertThat(four).isEqualTo(4);
Expand Down

0 comments on commit b9bad56

Please sign in to comment.