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 2017-04-06 00:28:34

johnpaul.dacop
Member
20 posts

johnpaul.dacop said:

Removing characters after the first occurrence of a character

Hello Anyone,
How do I remove characters after the first occurrence of a character(in this case "/") in a string?
Input: 3456439/3456432
Output: 3456439
Thanks! 

Offline

#2 2017-04-06 00:51:50

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: Removing characters after the first occurrence of a character

This will do it.....

input.substring(0, input.indexOf("/"))

....it will fall over if the "/" is not in the string though. So make sure you prepare for that before running it


rilhiaSolutionsLogo_0.png

Offline

#3 2017-04-06 01:46:13

johnpaul.dacop
Member
20 posts

johnpaul.dacop said:

Re: Removing characters after the first occurrence of a character

Hi rhall_2.0,
can you check my expression, I can't make it work:
row2.Type.compareToIgnoreCase("Telephone numbers")== 0 ?
row3.Country_Code + "-" + row3.Area_Code + "-" + StringHandling.RIGHT(row2.Telephone,7) :
row2.Type.compareToIgnoreCase("Contains 2 numbers")== 0 ?
row2.Telephone.substring(0,row2.Telephone.indexOf("/"))
: "-"
Thanks rhall_2.0!

Last edited by johnpaul.dacop (2017-04-06 01:47:30)

Offline

#4 2017-04-06 02:00:15

johnpaul.dacop
Member
20 posts

johnpaul.dacop said:

Re: Removing characters after the first occurrence of a character

It just came it my mind, I used StringHandling.LEFT(row2.Telephone,7)

Offline

#5 2017-04-06 07:31:08

TRF
Member
388 posts

TRF said:

Re: Removing characters after the first occurrence of a character

row2.Telephone.replaceAll("/.*$", "")

should works.

Regards,
TRF

Last edited by TRF (2017-04-06 08:43:19)

Offline

#6 2017-04-07 03:53:45

johnpaul.dacop
Member
20 posts

johnpaul.dacop said:

Re: Removing characters after the first occurrence of a character

Thanks TRF.
New question, I have data like this 4567890TO91. I already have "4567890" but how do I get "4567891" ?

Offline

#7 2017-04-07 13:04:54

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: Removing characters after the first occurrence of a character

Are you wanting to do this.....
Start with 4567890TO91
Take the number before the "TO" to give you 4567890
Look at the number after the "TO" and replace the end part of the previous number with the number after the "TO" to give you 45678 + 91
If so, this method will do the job. You will need to create a Routine and put this code into the Routine. Then you can use wherever you need this functionality. I should say that I knocked this up quite quickly and there *may* be more efficient ways of doing this. However this should get you on the right track

    public static String[] MyMethod(String input, String separator){
    	String[] returnVal = new String[2];
    	
    	//Check both input variables are not null or empty Strings
    	if(input!=null&&separator!=null&&input.compareToIgnoreCase("")!=0&&separator.compareToIgnoreCase("")!=0){
    		//Check the separator is in the input value
    		if(input.indexOf(separator)>-1){
    			String firstHalf = input.substring(0,input.indexOf(separator));
    			String afterSep = input.substring(input.indexOf(separator)+separator.length());
    			
    			//Set the first String
    			returnVal[0] = firstHalf;
    			
    			//Check the second String section is greater than 0 in length 
    			if(afterSep.length()>0){
    				//Check that the afterSep section is greater than or equal to the firstHalf
    				if(firstHalf.length()>=afterSep.length()){
    					returnVal[1]=firstHalf.substring(0, firstHalf.length()-afterSep.length())+afterSep;
    				}else{
    					returnVal[1] = afterSep;
    				}
    			}
    		}
    	}
    	
    	return returnVal;
    	
    }

rilhiaSolutionsLogo_0.png

Offline

#8 2017-04-07 13:34:40

TRF
Member
388 posts

TRF said:

Re: Removing characters after the first occurrence of a character

Hi,
The transformation rule is not clear but shortly you can do it with regex:

row21.phone.replaceAll("\\d{2}\\D{2}", "") 

Regards,
TRF

Offline

#9 2017-04-07 16:21:36

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: Removing characters after the first occurrence of a character

There is a difference between these two suggestions. If I read it correctly, TRF's solution will replace 2 digits followed by 2 chars with an empty String and leave you with....

45678 90TO 91 = 4567891
This will be far more efficient than what I gave.
The solution that I put together is a little more open to things like replacing the "TO" with any other String, with no value following the "TO" (of whatever) String, with no value preceeding that value and various lengths. For example, if you had.....

12TODAY99888


....the solution I put together would return 12 and 99888 (assuming that 99888 is bigger than 12 so therefore would replace the whole String). However, given what you gave it would produce 4567890 and 4567891 since the 91 is shorter than 4567890, so the number of chars that 91 takes up (2) is removed from 4567890 (45678) and the 91 is added.
The rules are these....
1) The separator value can be of any length greater than or equal to 1
2) The value after the separator value dictates how many characters are removed from the String before the separator value. So if the value after the separator value is 4 chars long,  then 4 chars will be removed from the right of the String before the separator value.
3) If the value after the separator value is greater in length than the value before the separator value, it completely overwrites the value before the separator value

Possibly a bit more than you wanted, but I saw it as a logical extrapolation of your example.



rilhiaSolutionsLogo_0.png

Offline

#10 2017-04-07 16:34:13

TRF
Member
388 posts

TRF said:

Re: Removing characters after the first occurrence of a character

Honored to have a challenge with rhall_2.0   wink
However, regex can be tuned for more flexibility, but as I said, we don't have expected rules.

Best regards,
TRF

Offline

#11 2017-04-07 16:39:48

rhall_2.0
Member
1251 posts

rhall_2.0 said:

Re: Removing characters after the first occurrence of a character

That wasn't meant as a challenge TRF :-)

I just noticed your suggestion and realised I should explain that what I was doing was covering an extrapolation of what was originally shown. Yours will be far more efficient than mine if the required logic is as you've used. 


rilhiaSolutionsLogo_0.png

Offline

Board footer

Talend Contributor Agreement - Talend Website Privacy Policy