Check if a field string start with a determinate string

hello to everyone,
i want to know how to create a screpted fields. the field is a string and I have to check if it starts with edi. if so the value of the field e EDI else "-".

how can I do. I need some help because I'm completely blocked.
thank you so much

Try this:

def path = doc['myField'].value;
if (path != null) {
    int loc = path.indexOf('edi');
    if (loc == 0) {
      return 'EDI';
    }
}
return null;

The myField field must be a keyword type (or use myField.keyword if you have the multi-field).

Personally, if the field is going to either be a certain value or not, I would probably return Booleans from my script, and call the field isEdi

HI it is perfect. Can you explain

 int loc = path.indexOf('edi');
    if (loc == 0) {
      return 'EDI';

so i can lear, i don't know the function that you used to solve my problem.

thank you so much
M

Sure.

indexOf is a method of the string variable type that takes a substring (edi) and returns back a number for the index of where that substring begins.

So if the string actually starts with edi, then the index of that substring will be 0, because the index value is 0-based. If the string doesn't contain that substring at all, then indexOf will return a -1.

When we find that the index of the substring is 0, then we return a value that becomes the value for the scripted field (EDI). If anything else happens, we return a null.

Fantastic, thank you so much. un other question, where i find documentation of a method as IndexOf.

thank you so much :grinning:

Painless is based on Groovy and is pretty much a subset of it. It restricts usage of the insecure methods and libraries, but usually you can refer to Groovy documentation to figure out how to do something:

http://docs.groovy-lang.org/latest/html/documentation/index.html#_manipulating_lists

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.