Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

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
binuacs
20 - Arcturus

@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 @binuacs 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 @binuacs  thanks for your help and all that have also helped!

Labels