[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
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
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();
}
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
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
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();
}
}
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