【问题标题】:Insert character in a string in dataweave 2.0在dataweave 2.0中的字符串中插入字符
【发布时间】:2021-11-08 03:53:31
【问题描述】:

输入:

{
  "string": "putbackthespaces",
  "indexes": [
    3,
    8,
    12
  ]
}

输出:

{
  "string": "put back the spaces"
}

我想在 payload.indexes 中的元素指定的特定索引处插入一个字符(在此特定示例中,将插入空格)。这里的问题是只能使用 mule 流和 dataweave 来完成转换。 谢谢!

【问题讨论】:

    标签: dataweave anypoint-studio mulesoft


    【解决方案1】:

    我使用递归函数为每个字符应用插入:

    %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 , " ")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      相关资源
      最近更新 更多