Browse Source

Document repeat and characer subtraction String operators in SpEL

Closes gh-32137
pull/32141/head
Sam Brannen 2 years ago
parent
commit
b9bad56fc1
  1. 83
      framework-docs/modules/ROOT/pages/core/expressions/language-ref/operators.adoc
  2. 22
      spring-expression/src/test/java/org/springframework/expression/spel/SpelDocumentationTests.java

83
framework-docs/modules/ROOT/pages/core/expressions/language-ref/operators.adoc

@ -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

22
spring-expression/src/test/java/org/springframework/expression/spel/SpelDocumentationTests.java

@ -1,5 +1,5 @@ @@ -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.
@ -283,14 +283,26 @@ class SpelDocumentationTests extends AbstractExpressionTests { @@ -283,14 +283,26 @@ class SpelDocumentationTests extends AbstractExpressionTests {
}
@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);

Loading…
Cancel
Save