如何在Data weave中实现Java的indexOf函数

How to Implement indexOf function of Java in Data weave

嗨,我有一个 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"