Skip to content

StringFormat

根据参数,按照格式化表达式的规则返回格式化后的字符串。

Lua字符串的字节下标是从 1 开始的

业务属性

属性含义说明输入限定示例值
enable是否启用该控件,未启用时不编译是否编译选择值启用
name控件返回值命名控件输出值变量名常量resultStr
text与控件关联的文本设计页控件显示常量StringFormat
expression字符串表达式常量/流程变量%s and %s
input指示参数多个输入值时以,分割常量/流程变量abc,#{formatStr}

编写字符串表达式的规则与绝大部分语言相同:由字符串常量指示组成,指示控制了每个参数放在字符串的什么位置,及如何放入它们。

常见的指示有:

  • %d:用于十进制数
  • %x:用于十六进制数
  • %o:用于八进制数
  • %f:用于浮点数
  • %s:用于字符串

使用示例

保留小数

表达式输入:%.4f(保留4位小数),参数输入:3.1415926

img.png

输出控件返回StringFormatdata属性为:

json
{
  "data": "#{stringFormat}"
}
{
  "data": "#{stringFormat}"
}
img_1.png

使用curl请求接口:

$ curl http://localhost:6636/api/stringFormat
$ curl http://localhost:6636/api/stringFormat

接口返回:

json
{
    "data": "3.1416"
}
{
    "data": "3.1416"
}

进制转换

表达式输入:%d %x %o(十进制数31转换成其他进制),参数输入:31,31,31

img_2.png

多个输入参数之间使用英文,进行隔开

使用curl请求接口:

$ curl http://localhost:6636/api/stringFormat
$ curl http://localhost:6636/api/stringFormat

接口返回:

json
{
    "data": "31 1f 37"
}
{
    "data": "31 1f 37"
}

格式化字符串

表达式输入:#{body.pattern},参数输入:#{body.day}

img_3.png

使用curl请求接口:

$ curl http://localhost:6636/api/stringFormat -d "{ \"pattern\": \"today is %s\", \"day\": \"Friday\" }"
$ curl http://localhost:6636/api/stringFormat -d "{ \"pattern\": \"today is %s\", \"day\": \"Friday\" }"

接口返回:

json
{
    "data": "today is Friday"
}
{
    "data": "today is Friday"
}