dataweave - 在dataweave 2.0中的字符串中插入字符
问题描述
输入:
{
"string": "putbackthespaces",
"indexes": [
3,
8,
12
]
}
输出:
{
"string": "put back the spaces"
}
我想在 payload.indexes 中的元素指定的特定索引处插入一个字符(在此特定示例中,将插入空格)。这里的问题是只能使用 mule 流和 dataweave 来完成转换。谢谢!
解决方案
我使用递归函数为每个字符应用插入:
%dw 2.0
output application/json
import * from dw::core::Arrays
fun insertChar(s, n, c)=s[0 to n-1] ++ c ++ s[n to -1]
fun insertAllChars(s, a, c)=
if (sizeOf(a)>0)
insertAllChars(
insertChar(s, a[0], c),
slice(a,1, sizeOf(a)),
c
)
else s
---
{
string: insertAllChars(payload.string, payload.indexes , " ")
}
推荐阅读
- python - 无法将 Python venv 克隆到另一台 PC
- java - 我正在尝试创建一个单独的 HTTPServer,旁边是一个用于管理和度量公开的运行。这是个好主意吗?
- php - 收到错误“参数 1 传递给 Modules\\Admin\\Events\\”
- mqtt - Azure IoT Hub MQTT 失败(无 SDK)
- c++ - 如何在 Visual C++ 项目中定义和加载资源?
- c++ - 不同类型之间的模板类分配
- php - 如何在 laravel 中显示具有 is_admin 属性的管理员列表?
- python - Python3 skimage:未定义符号:_gfortran_stop_numeric_f08
- javascript - 当页面首次加载或刷新 api post call 第一次不能正常工作,但在切换按钮一两次后效果很好
- javascript - 不确定现有项目是否正在使用其他数据格式的 blob 文件