如何解决如何在Data weave中实现Java的indexOf函数
嗨,我有一个类似的 java 函数
String firstName = appraiserName.substring(0,index);
String lastName = appraiserName.substring(index,stringLength);
在 Dataweave 中,我正在尝试如下,为什么我没有得到正确的值。我的期望是获得Vinoy的价值
var participantResult = [
{
"participantFirstName": "ParticipantVinoy","participantLastName" :"Vinoy VKP"
}
]
fun getIndexLastName(lastname) =
if(null != lastname and sizeOf(lastname find " ") > 0 )
lastname[0 to (sizeOf(lastname find " "))]
else
null
解决方法
使用字符串模块的 substringBefore() 和 substringAfter() 函数。会更容易。
https://docs.mulesoft.com/mule-runtime/4.3/dw-strings-functions-substringafter
https://docs.mulesoft.com/mule-runtime/4.3/dw-strings-functions-substringbefore
,添加到 Ale 的响应中,您可以在前后使用子字符串,也可以在“”上使用 splitBy 并选择结果数组的第一个元素。
例如在你的情况下:
%dw 2.0
output application/json
var participantResult = [
{
"participantFirstName": "ParticipantVinoy","participantLastName" :"Vinoy VKP"
}
]
fun getIndexLastName(lastname) =
if(null != lastname and sizeOf(lastname find " ") > 0 )
lastname[0 to (sizeOf(lastname find " "))]
else
null
---
//sizeOf(participantResult[0].participantLastName find " ") // result would be 1 and and thus your function would return Vi
//Try using substring before and after or splitBy - you can use this in your function
(participantResult[0].participantLastName splitBy(" "))[0]
输出:
"Vinoy"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。