One Star

How to use Substring and Instr function?

Hi ,
I am not sure what is the correct syntax for the substring and the instr used in the tmap component of talend.
I the string coming in the format like below:-
"Anatasio::50.00,Received"
Now I just need to extract 50.00 from the above sting not sure what is the correct syntax i will use in tmap component.
Please help on this
6 REPLIES
Seventeen Stars

Re: How to use Substring and Instr function?

You have to use the Java language:
row1.my_string.substring(row1.my_string.indexOf("::")+2, row1.my_string.indexOf(","))
returns your 50.00 value as String
If you need this as number:
Double.valueOf(row1.my_string.substring(row1.my_string.indexOf("::")+2, row1.my_string.indexOf(",")))
One Star

Re: How to use Substring and Instr function?

Thanks that works perfectly fine.
Just one more question if I need to apply the if else condition also. For eg. I have my incoming sting in two format:-
"Anatasio::50.00,Received"
"Michael wasnowaski ::70"
So I need to apply the condition
if my input sting has ', ' then
(Double.valueOf(row1.my_string.substring(row1.my_string.indexOf("::")+2, row1.my_string.indexOf(","))))
else
other function
So how do I write the if else function in the tmap component for this.
Seventeen Stars

Re: How to use Substring and Instr function?

You can do this with the ? operator.
<condition> ? <expression for true> : <expression for false>
You could also consider using regulary expressions. But this field is probably only useful if you decide to get deeper knowledge about that.
One Star

Re: How to use Substring and Instr function?

Hello guys,
what if I have a column with URL"s like:
http://www.mydomain-1.com/a/b/c/image1.jpg
http://www.mydomain-2.com/j/u/image2.jpg
.....
http://www.mydomain-x.com/f/r/y/w/image20000.jpg
The ulrs have not the same lenght, domains and subfolders.
How can I substract only the base name of the image, like:
image1.jpg
image2.jpg
...
image20000.jpg
Thank you for your help,
Lucian
Four Stars

Re: How to use Substring and Instr function?

Hi Lucian,
There is no straight forward way to do it using talend component, but you can easily do it it using Java way.
1) Import and use http://commons.apache.org/proper/commons-io/download_io.cgi commons-io.jar file using tlibrary load
2) Import library import org.apache.commons.io.FilenameUtils; or copy jar file in lib folder and restart talend
3) in Tjava use following code
String foo = "http://www.mydomain-1.com/a/b/c/image1.jpg";
String baseName = FilenameUtils.getBaseName(foo);
String extension = FilenameUtils.getExtension(foo);
System.out.println("file name is : " + baseName+"."+extension);
I think you got an idea, change your code to fit your requirement.
Thanks
Vaibhav
Seventeen Stars

Re: How to use Substring and Instr function?

hi all,
do not use java code but tFileProperties that contain base name information.
regards
laurent