[resolved] How to pass only the first 40 characters of a string using tMap?

One Star

[resolved] How to pass only the first 40 characters of a string using tMap?

Hi, my input has strings as long as 80 characters in a certain field
In the output field I only want up to the first 40 characters
How can I set this using the tMap Expression Builder?
Thank You Smiley Happy

Accepted Solutions
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

That method will make you run into a myriad of problems, as it will crash when that column is empty in a row, or when the string is shorter than 40 characters. Generally not what you want or need in a data integration solution.
You'll want to use String.format(format, inputString)
The format is a bit complex to use, so if you want to get more out of it, you'll have to google it yourself. However, for your problem, you'll need this:
"%.40s"
So the full method is:
String.format("%.40s", inputString)
In general, the format works like this for strings:
%<number1>.<number2>s
The % denotes an argument of the function. The first time it's the second argument (i.e. the one after the format), the next time it's the third, and so on. So you can write String.format("%.6s%.2s", LastName, FirstName) and you'll get a string consisting of the first six letters of the last name and the first two letters of the first name.
number1 is the minimum amount of characters in the string. The method will add spaces at the end.
The point is just a separator. It comes from the formatting of numbers, I think, where the number before the point is number of digits before the decimal point, and the number after is the number of digits after the decimal point.
number2 is the maximum amount of characters in the string. If there's more, they'll be cut off.
The s stands for string, other inputs require different identifiers here (e.g. f is for float).

All Replies
Moderator

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

Hi,
Try to use method substring(int beginIndex, int endIndex) in tMap expression.
See my screenshots
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

That method will make you run into a myriad of problems, as it will crash when that column is empty in a row, or when the string is shorter than 40 characters. Generally not what you want or need in a data integration solution.
You'll want to use String.format(format, inputString)
The format is a bit complex to use, so if you want to get more out of it, you'll have to google it yourself. However, for your problem, you'll need this:
"%.40s"
So the full method is:
String.format("%.40s", inputString)
In general, the format works like this for strings:
%<number1>.<number2>s
The % denotes an argument of the function. The first time it's the second argument (i.e. the one after the format), the next time it's the third, and so on. So you can write String.format("%.6s%.2s", LastName, FirstName) and you'll get a string consisting of the first six letters of the last name and the first two letters of the first name.
number1 is the minimum amount of characters in the string. The method will add spaces at the end.
The point is just a separator. It comes from the formatting of numbers, I think, where the number before the point is number of digits before the decimal point, and the number after is the number of digits after the decimal point.
number2 is the maximum amount of characters in the string. If there's more, they'll be cut off.
The s stands for string, other inputs require different identifiers here (e.g. f is for float).
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

Hi to both, thanks for the suggestions and explanation
I think for now I will address this problem in my SQL query instead then

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

ddctf,
If you are going to use Studio much, follow Elmer's advice when you have time to read up on it. Leaning on SQL for this kind of stuff will slow you down.
Talend people - someone (who you pay Smiley Happy) should write "Java for Talend." Even for people who know java, this kind of insight is important to get your product wider acceptance. Java's relation to Studio isn't just a great strength, it's your biggest obstacle, too. There's no reason you should let every new prospect figure out how to do this kind of thing themselves or cast Talend aside for some product with a stripped down pascal interpreter.
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

But honestly, Levin,
what is hard to learn on doing a "LEFT(<field>,<length>)" in SQL? Smiley Wink (even with the advance, that it's much faster)
But i also strongly recommend Elmers solution if you're working on large, long grown datastocks, you never now what you will get on the next run Smiley Wink
Greets,
M
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

But...
If one wants to use tMap and not SQL - why not StringHandling.LEFT(<field>,<length>) ?
It works with null value, empty string, strings shorter than expected length...
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

Hi All 
i have string like 'B0000790'
and result has to be like this 'B000    0790'
there are 4 spaces has to be added
please tell how can we do this
Help much appreciated
Thanks.
One Star

Re: [resolved] How to pass only the first 40 characters of a string using tMap?

You can achieve your required result using Talend functions something like this :-
StringHandling.LEFT(<YOUR_FIELD>, 4) + StringHandling.SPACE(4) + StringHandling.RIGHT(<YOUR_FIELD>, 4)

It might need a little refinement, but works OK in principle.