Bring your best ideas to the AI Use Case Contest! Enter to win 40 hours of expert engineering support and bring your vision to life using the powerful combination of Alteryx + AI. Learn more now, or go straight to the submission form.
Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

String Replace

ManuelRodrigues
8 - Asteroid

Hi Team,

 

I am looking to replace a particular word in a string "Home" [ Start Location] with the results in [City] and if it does not contain "home" leave it as is however i am struggling with wrapping this in a if statement with find and replace string formulae?

 

ManuelRodrigues_0-1643209263799.png

 

6 REPLIES 6
binu_acs
21 - Polaris

@ManuelRodrigues 

 

 

IIF(StartsWith([Start Location], 'Home'),Replace([Start Location], 'Home', [City]),[Start Location])

 

binuacs_0-1643209870651.png

 

Thableaus
17 - Castor
17 - Castor

Hey @ManuelRodrigues 

 

Something like this should work

 

IF Contains([Start Location], "HOME") THEN Replace([Start Location], "HOME", [City])

ELSE [Start Location] ENDIF

 

Cheers,

ManuelRodrigues
8 - Asteroid

thanks @Thableaus @binu_acs some of the strings have home in the middle of the string or end. sorry should have mentioned before. Would this still work?

 

Thableaus
17 - Castor
17 - Castor

@ManuelRodrigues 


If you want to avoid surprises, you could use REGEX Match function to identify word boundaries and just replace the full word "HOME", and not pieces of it inside other words.

 

Example: if a word contains "HOME" like homeless, homeboy, homework, the string shouldn't be replaced, right?

 

So you would use word boundaries (\b)

 

Thableaus_0-1643210543104.png

 

 

IF REGEX_Match([Field1], ".*\bHOME\b.*") THEN Replace([Field1], "HOME", [Field2]) ELSE [Field1] ENDIF

 

Cheers,

SoccerTil
8 - Asteroid

Don't forget the short version fuction for If ...Then ... Else ... EndIf 

 

IIF (
Contains([Start Location], "HOME")
, Replace([Start Location], "HOME", [City])
, [Start Location]
)

ManuelRodrigues
8 - Asteroid

@Thableaus @binu_acs  thanks for your help and all that have also helped!

Labels
Top Solution Authors