Page 1 of 1

Replacing a string with another string

Posted: Tue Oct 23, 2012 12:36 am
by aravindunni31
I have strings
MumbaiBangaloreChennai,
NaviMumbaiBangaloreChennai,
MumbaiPuneBangaloreKolkataChennai,
PuneBangaloreKolkataChennai,

I need to replace Kolkata with Calcutta if it exists in the string. How it can be achieved? This is in Datastage 8.1

Re: Replacing a string with another string

Posted: Tue Oct 23, 2012 1:39 am
by prasson_ibm
Hi,

I think using Index function,u can achieve your requirment.Try serching in this forum,you will get multiple threads.

Thanks
Prasoon

Posted: Tue Oct 23, 2012 2:33 am
by aravindunni31
Thanks Prasoon. This will solve my query.

Posted: Tue Oct 23, 2012 6:43 am
by chulett
No, it will help you find the string but does nothing to replace it. How are you planning on doing the actual replacement?

Posted: Tue Oct 23, 2012 7:01 am
by ray.wurlod
Use the pxEreplace() function (which you can find on DSXchange) or upgrade to version 9.1 and use Ereplace() or Change() function.

Posted: Tue Oct 23, 2012 7:33 am
by bhasds
Hi aravindunni31,
The below logic may be help full-

Code: Select all

If Index(InputCol,"Kolkata",1) =0 Then InputCol Else InputCol [1,Index(InputCol,"Kolkata",1)-1] :"Calcutta":InputCol [Index(InputCol,"Kolkata",1) +7, Index(InputCol, Right(InputCol,1),1)]
Please correct me if I am wrong.

Posted: Tue Oct 30, 2012 5:58 am
by aravindunni31
Thanjs . Will try it

Posted: Tue Oct 30, 2012 5:59 am
by aravindunni31
ray.wurlod wrote:Use the pxEreplace() function (which you can find on DSXchange) or upgrade to version 9.1 and use Ereplace() or Change() function. ...
I am doing this in a parallel job using a transformer. EReplace function is not there. Can you please tell me what are the functions used to achieve this

Posted: Tue Oct 30, 2012 7:45 am
by chulett
ray.wurlod wrote:Use the pxEreplace() function (which you can find on DSXchange)

Posted: Mon Nov 05, 2012 7:53 am
by PhilHibbs
Sadly I must advise that you DO NOT use the pxEreplace function. This is from IBM support:
IBM wrote:Good Morning,

I did some further research and found some more information:

It would seem that there is no way to free the memory that is allocated in a C transform routine. You should not use malloc in a transform routine.

We suggest that if you have a complex function then you may want to consider using a BuildOp instead. Calling C Transform routines is really meant for routines that return native C data types (primitive data types). Complex functions should be created as BuildOp's or Custom C stages.

I hope this advice helps

Many thanks
Mark H
This confirms my suspicion that the char* return type is almost entirely useless. The only safe thing that you can return with a char* routine is a fixed constant string such as an error message. It baffles me why IBM do not allow std::string as a return type, as this is precisely the reason that std::string was invented.

So, how do BuildOps work? I've never done one.

Getting back to the OP's problem - for simple cases, where you are sure that you have only one occurrence of the search string, then the index function can be used to find the string and you can slice it yourself in a derivation. Something like this, adjust slightly if your string might be longer than 999 characters:

Code: Select all

=If index(input,"Kolkata") = 0 Then input Else input[1,index(input,"Kolkata")-1] : "Calcutta" : input[index(input,"Kolkata")+7,999])
The index(input,"Kolkata") call should probably be moved out into a stage variable for efficiency.

Re: Replacing a string with another string

Posted: Tue Nov 06, 2012 3:54 pm
by saidvs
try this

Convert('KolkataMumai','Calcutta','Kolkata') it works in parallel transformer too..

Re: Replacing a string with another string

Posted: Wed Nov 07, 2012 3:02 am
by aravindunni31
Convert Wont work for this scenario. Convert will convert only for one character. Not string

Re: Replacing a string with another string

Posted: Wed Nov 07, 2012 7:00 am
by PhilHibbs
saidvs wrote:try this
Convert('KolkataMumai','Calcutta','Kolkata') it works in parallel transformer too..
That will convert the third parameter replacing 'K' -> 'C', 'o's -> 'a', 'k' -> 'c', 'a' -> 'u', etc., and removing all 'M', 'u', 'm' and 'i' characters. Not exactly what the OP wanted.

Posted: Wed Nov 07, 2012 2:58 pm
by ray.wurlod
If you're not going to use pxEreplace() then you need a combination of Index() function and substring operators. For example:

Code: Select all

sv1  <--  Index(InLink.TheString, "Kolkata", 1)
sv2  <--  If sv1 Then InLink.TheString[1,sv1-1] : "Calcutta" : InLink.TheString[sv1+7,Len(InLink.TheString)-7]