【问题标题】:Is it possible to multiply the string 10 with number 10是否可以将字符串 10 与数字 10 相乘
【发布时间】:2019-04-17 05:38:34
【问题描述】:

乘以'10' * 10 得到一些输出值10101010101010101010

任何人都可以证明这一点吗?

ExpressionParser parser = new SpelExpressionParser();
System.out.println(parser.parseExpression("'10' * 10").getValue());

输出:10101010101010101010

【问题讨论】:

  • 你期待什么输出?
  • 您对表达式“string '10' 十次”有什么期望?
  • 试试 Integer.parseInt()
  • @Eran == > 它应该抛出一些异常,因为在 java 中我们不能将字符串与数字相乘。
  • Spring 表达式语言 (SpEL) != Java

标签: java spring spring-el


【解决方案1】:

它应该抛出一些异常,因为在 java 中我们不能将字符串与数字相乘。

SpEL 不是 Java 它有一些相似之处,但它不是 Java。它没有 lambda,它有很多不同的语法。

应用于字符串的乘数运算符意味着将字符串连接该次数。

类似于'10' + '10' = '1010''10' * 2 = '1010'

OpMultiply 类中的 Javadoc:

/**
 * Implements the {@code multiply} operator directly here for certain types
 * of supported operands and otherwise delegates to any registered overloader
 * for types not supported here.
 * <p>Supported operand types:
 * <ul>
 * <li>numbers
 * <li>String and int ('abc' * 2 == 'abcabc')
 * </ul>
 */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多