Remove portion of string under certain condition

Hello,

I have a column with strings lookig like this :

ABDC01234
ABCD56789

I would like to replace the 6th positon of the strinf with an / but only if its a zero. So I would like my result to look like this

ABCD/1234
ABCD/56789

So far I used the string manipulation node
join(substr(string($Columnname$),0,6), β€œ/”, substr(string($Columnname$),6))

but this just inserts the /. Has anybody an idea how to do that?
Sorry if this question was already asked, I couldnt find anything.

Thanks in advance

Hello @Francesca,

this expressions should replace 0 on 6th position with /
join(substr($col$,0,5), replace(substr($col$,5,1),"0" ,"/"), substr($col$,6))

Br,
Ivan

Thank you but it doesn’t solve my problem entirely. Because it doesn’t insert the / for strings with no zero. so the result is

ABCD/1234
ABCD56789

but I want
ABCD/1234
ABCD/56789

Hello @Francesca,

I see now. Then you can use Column Expressions node with following expression:

if( substr(column(β€œcol”),5,1) == β€œ0”) {
join( substr(column(β€œcol”),0,5 ), β€œ/”, substr(column(β€œcol”),6))
}
else
{
join( substr(column(β€œcol”),0,5 ), β€œ/”, substr(column(β€œcol”),5 ))
}

Br,
Ivan

2 Likes

Hi
not sure if your string is more complicated then your examples but if not then

regexReplace(substr($column1$,0 ,4 ) +"/"+substr($column1$,4),"/0","/") with string manipulation could do the job
done

(normal replace should also work i suppose)

2 Likes

Thanks Ivan and Daniel for your solutions. Both worked!

I have another questions regarding string manipulation. What if I wanted to insert the β€œ/” not at a certain position but for example after the second blank space?

So my strings would look like
AB CD 1234
AB F 1234

and I wanted to get

ABCD/1234
ABF/1234

is that also possible with the regexreplace?

Hello @Francesca,

here is one that should hopefully do the trick:
regexReplace($column1$,"(\\w+) (\\w+) (.*)" ,"$1$2/$3" )

In case there is more than 3 groups separated with spaces use removeChars() function around above expressions to remove all space characters.

Br,
Ivan

2 Likes

Thank you Ivan! That solves my problem :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.