Hi guys, I have the following path url/records/848669637/names/1756
I need to substring 1756 as resource, how can I achieve this? transformer substring doesn’t seem to have indexOf to select my start, the lenght could change and it does not work.
Not necessarily easier (mainly a matter of taste) but another option is to use
pub.string:tokenize
which you can use to get all parts delimited by a specific character (e.g. /) as a string list. You just need to get the last entry of the list. This comes in handy especially if you need more parts than only one.
I was going to suggest tokenize as an alternative, but the array index of the last element varies dynamically depending on the number of resources in the URL, so a static map will be a problem.
A lastIndexOf service would have been quite useful , lastIndexOf(“/”) , followed by a subString would have done the trick quite easily. @Martin_Wroblinski , @Venkata_Kasi_Viswanath_Mugada1 - have you seen such a requirement many other times?
I’m quite sure, everybody faced this and just worked around, as it is quite easy, nevertheless I agree, as getting the last part (like filename or extension) is a really common use case.
About the list out of the tokenize service: pub.list:sizeOfList gives you the size of the list, so just substract one and do an index map (you probably know this, but for the others):
When mapping from a list to a single element, you can click the mapping line and enter an index, to select one element to be mapped. This index can be a runtime variable.
You are right of course, @Martin_Wroblinski; I was only referring to tokenize as a standalone step
@Nagendra_Prasad, yes I’ve had numerous requirements for string manipulation utilities that I had to build.
The most frequent one that I’ve used is a “search” type functionality to capture the number of ALL occurrences and positions, of a given substring (across multiple language encodings) in a single execution (link).
In general, I avoid tokenize and instead use split. tokenize has some behaviors that may not be desired, depending on the specific needs. I won’t list the differences here – plenty of good material on the web covers that.
WmPublic does not have a split service. But it is trivial to create one in your own “public” package.
Other useful services for your own public package would be splitPath, leveraging java.nio.file.Paths and Path classes to do the work. And splitURL, leveraging java.net.URL.