Substring in Talend

Six Stars

Substring in Talend

22.33.442.555.0

 

Please help me to get each value separated which is separated by dot 

 

ip1 = 22

ip2 = 33 

ip3 = 442

ip 4 = 555


Accepted Solutions
Sixteen Stars

Re: Substring in Talend

You are dealing with String data (text). You need to convert it to an Integer before you can carry out numeric calculations with it. You need to "cast" your String values to int values. Take a look here: https://www.mkyong.com/java/java-convert-string-to-int/

 

But in your scenario you need to do something like this....

 

Integer.parseInt(((String[])Var.var1)[0]) * 16777216

This was actually a different question to the titled question. You should have really raised a different question as it can get confusing when multiple questions are raised under a misleading title. 


All Replies
Forteen Stars TRF
Forteen Stars

Re: Substring in Talend

Six Stars

Re: Substring in Talend

In a file i have data as below , it is comma delimited .  

111.5689.7.0,9.56.783.21,"XX","05","Glen","5004"

 

But the first column is 111.5689.7.0 so i need to get each value which is separated by dot 

Sixteen Stars

Re: Substring in Talend

You can use a basic Java String method for this called "split". It is described here....

https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#split-java.lang.String-

 

But as an example, if your column is called "value" and your row is called "row1", you would use it like this....

String[] splitVals = row1.value.split("\\.");

You can then retrieve the values using the String[] like below....

String firstToken = splitVals[0];
String secondToken = splitVals[1];
String thirdToken = splitVals[2];
Six Stars

Re: Substring in Talend

I am trying to achieve using tmap component

Six Stars

Re: Substring in Talend

basically trying to get the position number of the nth occurence of the character in a string 

 

example 

111.5689.7.0

 

first dot should return position = 4

second dot should return  position =  9

third dot should return position = 11

Sixteen Stars

Re: Substring in Talend

The method I gave you will work. You do not need to know where the "." is. In a tMap variable (of class Object), out this code....

row1.value.split("\\.") 

Then in your tMap output table, retrieve the values in their respective columns like this...

((String[])Var.myObjectVariable)[0] 
((String[])Var.myObjectVariable)[1] 
((String[])Var.myObjectVariable)[2] 
((String[])Var.myObjectVariable)[3] 
Six Stars

Re: Substring in Talend

Hi 

 

After getting each value i tried below , but compilation failed 

Detail Message: The operator * is undefined for the argument type(s) String, int

 

(((String[])Var.var1)[0] * 16777216) + (((String[])Var.var1)[1] * 65536) + (((String[])Var.var1)[2] * 256 ) + (((String[])Var.var1)[3]) 

Sixteen Stars

Re: Substring in Talend

You are dealing with String data (text). You need to convert it to an Integer before you can carry out numeric calculations with it. You need to "cast" your String values to int values. Take a look here: https://www.mkyong.com/java/java-convert-string-to-int/

 

But in your scenario you need to do something like this....

 

Integer.parseInt(((String[])Var.var1)[0]) * 16777216

This was actually a different question to the titled question. You should have really raised a different question as it can get confusing when multiple questions are raised under a misleading title. 

Six Stars

Re: Substring in Talend

Hi 

 

I tried but result is not correct 

example 

217.233.0.0

 

217 * 16777216 = 3640655872

 

but output from talend is as =  -654311424

Sixteen Stars

Re: Substring in Talend

You are working with an int. An int's max size is 4 bytes or 32 bits. This equates to a range of −2,147,483,648 to 2,147,483,647. This is why your number is wrong. You should probably work with BigDecimals to work with calculations this big.