Arcade expression to find text in string and assign value

11493
4
Jump to solution
04-07-2019 10:18 PM
CPoynter
Occasional Contributor III

Hi,

I have a field that I wish to assign classes too (e.g. ABC123 - W - 201930 - 4/03/2019 to 21/06/2019 - D). I wish to search the text string using Arcade, so that I can classify my data into three unique classes.

I have found the 'Find' function, but unsure about how to use it correctly.

var placement = When(


Find('ABC123', $feature["USER_Unit_Offering_Display_Text"]),'Field 1',
Find('DEF456', $feature["USER_Unit_Offering_Display_Text"]),'Field 1',
Find('GHI789', $feature["USER_Unit_Offering_Display_Text"]),'Field 2',
Find('JKL012', $feature["USER_Unit_Offering_Display_Text"]),'Field 2',
Find('MNO345', $feature["USER_Unit_Offering_Display_Text"]),'Field 2 (Ongoing)', ' '
)

return placement;

Any ideas please?

Regards,

Craig

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You could try something like this:

var disp_txt = $feature["USER_Unit_Offering_Display_Text"];

if (Find("ABC123", disp_txt, 0)>-1) {
    return "Field 1";
} else if (Find("DEF456", disp_txt, 0)>-1) {
    return "Field 1";
} else if (Find("GHI789", disp_txt, 0)>-1) {
    return "Field 2";
} else if (Find("JKL012", disp_txt, 0)>-1) {
    return "Field 2";
} else if (Find("MNO345", disp_txt, 0)>-1) {
    return "Field 2 (Ongoing)";
} else {
    return "Other";
}

If the function Find does not find the specified string it will return the value -1

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

You could try something like this:

var disp_txt = $feature["USER_Unit_Offering_Display_Text"];

if (Find("ABC123", disp_txt, 0)>-1) {
    return "Field 1";
} else if (Find("DEF456", disp_txt, 0)>-1) {
    return "Field 1";
} else if (Find("GHI789", disp_txt, 0)>-1) {
    return "Field 2";
} else if (Find("JKL012", disp_txt, 0)>-1) {
    return "Field 2";
} else if (Find("MNO345", disp_txt, 0)>-1) {
    return "Field 2 (Ongoing)";
} else {
    return "Other";
}

If the function Find does not find the specified string it will return the value -1

MoyaCalvert_01
New Contributor II

Dear Xander,

I have tried your code for something similar.  The code tests ok, but when I hit 'Done', I get a message that says 'WARNING.  the expression must return a number value'.  But its a text field?  

I am applying the expression to a layer style (hosted feature service) so i can get a unique symbology (originally, the values were controlled by a domain but now its free text so I have to use code to classify the various inputs).  Code is: 

//RWW reproductive status.
//Group by flowering, or not flowering
//6 classes
var Repro = $feature.REPRSTATUS;

if (Find("Flowering",Repro,0)>0) {
return "Flowering"}
else if (Find("Non",Repro,0)>0) {
return "Non flowering"}
else if (Find("Veg",Repro,0)>0) {
return "Non flowering"}
else if (Find("Immature",Repro,0)>0) {
return "Non flowering"}
else if (Find("Seed",Repro,0)>0) {
return "Seeding"}
else {
return "Not recorded"
}

Any help would be appreciated:)

0 Kudos
MoyaCalvert_01
New Contributor II

Fixed.  I already had a symbology applied to the layer, so was (unintentionally) trying to apply a 2nd symbology, which must be for numbers only.  It worked fine once I removed the first symbology.  

0 Kudos
CPoynter
Occasional Contributor III

Thank you Xander,

Code example you have provided worked excellent.

Regards,

Craig