You are not logged in.

Unanswered posts



Important! This site has been replaced. All content here is read-only. Please visit our brand-new community at https://community.talend.com/. We look forward to hearing from you there!



#1 2016-06-14 09:41:24

Deniz Capkan
Member
4 posts

Deniz Capkan said:

tReplace characters after appearing the 3rd time in a String

Dear Talend Specialists,

I would need your help on the following issue:

I have Strings looking like this
31,2,12345,77
33,51,2324,5454,44
What I need:
31,2,1234577
33,51,2324545444

=> I need to get the commas removed after the second comma (keeping the first 2 commas in the String)
I did not get this working with treplace.
Any ideas welcome

Thc in advance
BR
Deniz

Last edited by Deniz Capkan (2016-06-14 09:41:53)

Offline

#2 2016-06-14 11:28:34

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: tReplace characters after appearing the 3rd time in a String

This will do it.....

public static String removeFromNthInstance(String data, String val, int instanceNo){
    	
    	int count = 0;
    	
    	for(int i=0; i<data.length(); i++){
    		String tmpVal = data.substring(i, i+1);
    		if(tmpVal.compareToIgnoreCase(val)==0){
    			count++;
    			if(count>=instanceNo){
    				data = data.substring(0, i)+data.substring(i+1);
    				i--;
    			}
    		}
    		
    	}
    	return data;
    }

The "data" param is your original value, the "val" param is the value to remove and the "instanceNo" is the instance of the value to remove from. 

This isn't thoroughly tested, so use this as a basis to start from


rilhiaSolutionsLogo_0.png

Offline

#3 2016-06-14 15:55:21

Deniz Capkan
Member
4 posts

Deniz Capkan said:

Re: tReplace characters after appearing the 3rd time in a String

THx for that
your code makes sense, but I don´t know how to link it to my Talend-Job :-(
I tried with a tJava, but did not get it working.

I guess conceptually there is sth. wrong in my approach

Last edited by Deniz Capkan (2016-06-14 15:55:46)

Offline

#4 2016-06-14 19:08:28

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: tReplace characters after appearing the 3rd time in a String

You need to create a code routine and add that as a method. You can then use it in a tMap, etc.


rilhiaSolutionsLogo_0.png

Offline

#5 2016-06-16 08:08:08

Deniz Capkan
Member
4 posts

Deniz Capkan said:

Re: tReplace characters after appearing the 3rd time in a String

Now I created the routine, which does not throw any errors.
I ran the method in a syso in Eclipse as well and confirm it works as expected.

Only issue now is that it does not work in Talend:
If I call the method from this routine, handing over Parameters like (used in a tMap):
remove2.removeFromNthInstance(row6.telephoneNumber,",",3)
I get a NullPointerException, that´s really weird.
Using an existing Method like StringHandling.Upcase(row6.telephoneNumber) works fine.
Do you have any further ideas?

Last edited by Deniz Capkan (2016-06-16 08:09:06)

Offline

#6 2016-06-16 10:56:47

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: tReplace characters after appearing the 3rd time in a String

That will likely be because your column data is null. I didn't handle null column data scenarios. All you need to do is add some code to handle null "data" values.


rilhiaSolutionsLogo_0.png

Offline

#7 2016-06-16 15:41:06

Deniz Capkan
Member
4 posts

Deniz Capkan said:

Re: tReplace characters after appearing the 3rd time in a String

Ok got it working now:
just added a regex to the tmap, where I call the method :-)
row3.distinguishedName!=null

sometimes it takes ages to solve minor issues..

thanks for you help,

Have a nice day

Offline

#8 2016-06-16 15:56:23

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: tReplace characters after appearing the 3rd time in a String

Glad you have it working :-)


rilhiaSolutionsLogo_0.png

Offline

Board footer

Talend Contributor Agreement - Talend Website Privacy Policy