Select to view content in your preferred language

Replace a 0 with text if no other numbers are in the same field

1048
4
Jump to solution
06-08-2022 12:20 PM
Labels (2)
pointi12
New Contributor

Hey Guys!

As the title already says, I am trying to replace a singe digit (In my case zero) if there is no other number in the same field. The column is "string"

I already tried this:  Replace ($feature.test, '0', 'test')

Unfortunately, it replaced all zeros, which I don't want.

Thanks a lot!!

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

You could use Iif and check the explicit value using the "==" operator. That way only features where the field is equal to 0 get the replacement text, but will otherwise return the original  value.

return Iif($feature.test == '0', 'test', $feature.test)

 

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

You could use Iif and check the explicit value using the "==" operator. That way only features where the field is equal to 0 get the replacement text, but will otherwise return the original  value.

return Iif($feature.test == '0', 'test', $feature.test)

 

- Josh Carlson
Kendall County GIS
SteveCole
Honored Contributor

Maybe someone else can code a more elegant evaluation but based on your sample screenshot, it looks like you need to verify that only *one* zero is present in the value so you need to evaluate all the characters in the value before deciding whether or not to alter its value. Something like thisL

 

var counter = 0;
var stringLen = 0;
var theString = "23405751";

stringLen = Count(theString) - 1;

for(var z=0; z<stringLen; z++) {
  if (Mid(theString,z,1) == "0") {
      counter++;
  }
}

if (counter > 1) {
    return 'There was more than one zero';
} else {
    return 'There was only one zero';
}

 

Paste this code into the Playground and add/subtract zeros from the string to see it.

0 Kudos
e_mcc
by
Occasional Contributor

Hi,

 

As Steve said, you need to make sure you are changing the 0-only numbers, in other words, a single-digit number/string of a zero will have a value of 0!  

Convert the column string to a number and if its value=0 then this means it's a one-digit string of zero>>>replace it, otherwise, return the original value!

 

Return Iif(Number($feature.test)==0, "test" , $feature.test)

 

Cheers,

0 Kudos
pointi12
New Contributor

Thanks a lot, guys!

I was able to change it.

0 Kudos