Arcade function to remove characters from string except numbers

4440
2
Jump to solution
03-10-2020 02:46 PM
JoeBryant1
Occasional Contributor III

Hi,

I have a string field that contains both a test number and a Hydrant Test ID (alpha character) that I'm attempting to split into two separately returned values for a Survey123 URL Parameter. I'm currently using the Custom Attribute Display in the Pop-Up for the feature instead of calculating a new field (the feature layer is joined to a hosted table, so I can't create and calculate a new field).

The field values are formatted like this: 1, 1A, 1B, 2, 2A,......14, 14A, etc.

I'm trying to return both the Test Number (1, 2, 14) and the Hydrant ID (A, B, C, D, else "Flowing") separately.

Returning the ID I have figured out :

   

var HydrantID = Right($feature["WY_TestNum"], 1)

IIF($feature["WY_Status"]=="Flowing", "Flowing", HydrantID)

I thought I had the Test Number figured out using the Number function:

Number($feature.WY_TestNum)

But this only works for the strings that only contain a number. Values like '1A' don't return anything ('NaN').

Is there a way to use the pattern parameter to return just the number values, even though my data pattern is not index-able from left to right?

Tags (1)
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hey Joe Bryant‌,

One way you could approach this would be to define your own function that iterates through the characters in a string and returns only the characters that are found to be numbers.

In the example below I'm testing each character of the string individually to see the Number function returns NaN - then constructing a new string from only characters where a Number() actually returned a number. 

function ExtractNumbers(string){
    var result = "";
    for (var i=0; i<Count(string); i++){
        if(!isNan(Number(string))){
            result += string;
        }
    }
    return result; 
}

ExtractNumbers("1A");

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hey Joe Bryant‌,

One way you could approach this would be to define your own function that iterates through the characters in a string and returns only the characters that are found to be numbers.

In the example below I'm testing each character of the string individually to see the Number function returns NaN - then constructing a new string from only characters where a Number() actually returned a number. 

function ExtractNumbers(string){
    var result = "";
    for (var i=0; i<Count(string); i++){
        if(!isNan(Number(string))){
            result += string;
        }
    }
    return result; 
}

ExtractNumbers("1A");
JoeBryant1
Occasional Contributor III

Thanks James! That absolutely worked!

It was also a lot more difficult than I expected. I'll have to save that function as I doubt I could write it from scratch.

0 Kudos