[resolved] keep last part of string after a character (or substring)

Six Stars

[resolved] keep last part of string after a character (or substring)

Hi,
Trying to get the last part of a string, in this case it's a url:
http://mydomain.com/abc-defg/filename
How can I get the last part, 'filename' or in other words: the part of string behind the last slash character "/"
I looked around on the forum for stringhandling but found only clear descriptions to get the left or right part after the first occurrence of "/". Also found that there exist some stringUtils in exchange but those seem not available in the version I'm using (5.3.0.r101800). Perhaps only for earlier versions.
Best Regards,
Henry

Accepted Solutions
Six Stars

Re: [resolved] keep last part of string after a character (or substring)

Thank you so much Janhess,
I missed the 'public class my_routines {' part in my first try, discovered the mistake thanks to your example.
If others also stumble on this thread, I used the routine by adding the expression below in tMap.
my_routines.reverseIt(StringHandling.LEFT((my_routines.reverseIt(row8.imageUrl)),StringHandling.INDEX((my_routines.reverseIt(row8.imageUrl)) ,"/")))
Perhaps the expression can be made easier I don't know, but the end result is exactly the last part after the "/", as intended.
Many thanks, how should I indicate as resolved?
Henry

All Replies
One Star

Re: [resolved] keep last part of string after a character (or substring)

You could use this routine to reverse the string then get the left part then reverse the result.

/**
* reverseIt() Reverses a string.
*
* {talendTypes} String
*
* {Category}my_routines
*
* {param} string ("fred") input: the string to be reversed
*
* {example} reverseIt("fred") # derf
*
*/
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--)
dest.append(source.charAt(i));
return dest.toString();
}
Six Stars

Re: [resolved] keep last part of string after a character (or substring)

Many thanks for you quick response, that makes sense. Since I'm quite new with Talend, where should I put the code in, a routine? I might need some exact guidance since that would be the first time, so far managed to do all with the built in components
Six Stars

Re: [resolved] keep last part of string after a character (or substring)

sorry you already said 'use this routine' guess I need to dive into routines then, as simple copy paste of the code in routines gets me syntax error warnings in the routine code screen
One Star

Re: [resolved] keep last part of string after a character (or substring)

create a routine called my_routines and paste the code in to replace what's auto generated for the example routine.
This gives
package routines;
/*
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
* it must be before the "{talendTypes}" key.
*
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
* Short
*
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
*
* 4. {param} 's format is: {param} <type> <name>
*
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
* added. you can have many parameters for the Function.
*
* 5. {example} gives a example for the Function. it is optional.
*/
public class my_routines {
/**
* reverseIt() Reverses a string.
*
* {talendTypes} String
*
* {Category}my_routines
*
* {param} string ("fred") input: the string to be reversed
*
* {example} reverseIt("fred") # derf
*
*/
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--)
dest.append(source.charAt(i));
return dest.toString();
}
}
Six Stars

Re: [resolved] keep last part of string after a character (or substring)

Thank you so much Janhess,
I missed the 'public class my_routines {' part in my first try, discovered the mistake thanks to your example.
If others also stumble on this thread, I used the routine by adding the expression below in tMap.
my_routines.reverseIt(StringHandling.LEFT((my_routines.reverseIt(row8.imageUrl)),StringHandling.INDEX((my_routines.reverseIt(row8.imageUrl)) ,"/")))
Perhaps the expression can be made easier I don't know, but the end result is exactly the last part after the "/", as intended.
Many thanks, how should I indicate as resolved?
Henry
One Star

Re: [resolved] keep last part of string after a character (or substring)

Hello guy,
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
Five Stars

Re: [resolved] keep last part of string after a character (or substring)

Don't forget that the String Class has the method String.lastIndexOf, so you could simply use this with String.substring.
One Star

Re: [resolved] keep last part of string after a character (or substring)

Hi,
You're obliged to create your function and add it to your talend studio, in repository>code>routine

When your fonction added you will find it in tMap in the category "defined by user".

This is a simple function in java that i have made on Eclipse, i have tested on two exemple and i have the right result that you are looking for.

String Str = new String("http://www.mydomain-1.com/a/b/c/image1.jpg");

System.out.print("Found Index :" );
System.out.println(Str.lastIndexOf("/"));
int beginIndex = Str.lastIndexOf("/")+1;

Str = Str.substring(beginIndex);
System.out.println(Str);

String Str1 = new String("http://www.mydomain-x.com/f/r/y/w/image20000.jpg");

System.out.print("Found Index :" );
System.out.println(Str1.lastIndexOf("/"));
int beginIndex1 = Str1.lastIndexOf("/")+1;

Str1 = Str1.substring(beginIndex1);
System.out.println(Str1);
results :
Found Index :31
image1.jpg
Found Index :33
image20000.jpg

in your class you can use this function.

public static String deleateSuboldersFromUrl(String str) {

return str.substring(str.lastIndexOf("/")+1);

}
It is an idea and you can adapt and optimize it.
Best regards