One Star

How to use String.split function

Hi All
How to use String.split function in tmap expersion?
Regards
Kranthi
10 REPLIES

Re: How to use String.split function

Hello aishu,
Create a routine or function to return the string array and use that function in tMap or you can also use this split function in tJavaFlex component to derive new columns.
One Star

Re: How to use String.split function

Hi vikram,
can you post split routine code.
Thanks for your reply.
Regards
Aishu
Moderator

Re: How to use String.split function

Hi Aishu,
Could you elaborate your case with an example with input and expected output values? Maybe I can design a demo job with expression in tMap for you .
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: How to use String.split function

Hi Sabrina,
Source table have 2 fields like
Telephone_1 , Telephone_2.
This 2 fields have data like
Telephone_1 Telephone_2
-------------- ----------------
9999999ext29 88888888ext30
8888888ex30 666666666ext40
My requirement is if Telephone_1,Telepohe_2 contains extension number,
we split Telephone_1,Telephone_2 field data and insert the extension number
data into another fields.
output like
========
Telephone_1 Telephone_1_extension Telephone_2 Telephone_2_extension
--------------- -------------------------- --------------- ---------------------------
9999999 29 888888888 30
8888888 30 666666666 40
I used tExtractdelemitedFields component for this requirement , it is working fine.
source table have 5 fields , so i used 5 times tExtractdelemitedFields component in my job.
Is there any alternate solution?

Regards
Aishu
One Star

Re: How to use String.split function

Hi Sabrina,
Source table have 2 fields like
Telephone_1 , Telephone_2.
This 2 fields have data like
Telephone_1 Telephone_2
-------------- ----------------
9999999ext29 88888888ext30
8888888ex30 666666666ext40
My requirement is if Telephone_1,Telepohe_2 contains extension number,
we split Telephone_1,Telephone_2 field data and insert the extension number
data into another fields.
output like
========
Telephone_1 Telephone_1_extension Telephone_2 Telephone_2_extension
--------------- -------------------------- --------------- ---------------------------
9999999 29 888888888 30
8888888 30 666666666 40
I used tExtractdelemitedFields component for this requirement , it is working fine.
source table have 5 fields like Telephone_1 ,Telephone_2,Telephone_3,Telephone_4
Telephone_5 so i used 5 times tExtractdelemitedFields component in my job.
Is there any alternate solution?

Regards
Aishu
Moderator

Re: How to use String.split function

Hi,
Telephone_1 Telephone_2
-------------- ----------------
9999999ext29 88888888ext30
8888888ex30 666666666ext40

From your source file, what is your Field Separator in tExtractDelimitedFields?
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.

Re: How to use String.split function

Hello aishu,
You can create a function to split the telephone number and extension. function should return the string array. And then you can use the array to assign the values to required columns using tJavaFlex.
One Star

Re: How to use String.split function

Hi Sabrina,
If Telephone_1 field contains ext,ex.
I replace ext,ex as ';' using tReplace component,
and i used semicolon ';' as a Field Separator in tExtractDelimitedFields.
My job design like:
tpostgresssqlinput----------->tReplace----------->tExtractDelemitedFields---------->tlogrow
Thanks for your replay
Regards
Aishu
Seven Stars

Re: How to use String.split function

To use the .split method, which is what tExtractDelimitedFields uses, you do not need to replace the "ext" and "ex" with ";". It will work fine with "ext?" as the Field Separator. The manual way to use it in an expression would be:
row1.Telephone_1.split("ext?") for the phone number
row1.Telephone_1.split("ext?") for the extension
But when there is no extension, this will cause an ArrayIndexOutOfBoundsException. So you would need code like the following (in tJavaRow):
String[] parsedNumber = input_row.Telephone_1.split("ext?");
output_row.Telephone_1 = parsedNumber;
if (parsedNumber.length>1) {
output_row.Telephone_1_extension = parsedNumber;
} else {
output_row.Telephone_1_extension = null;
}
parsedNumber = input_row.Telephone_2.split("ext?");
output_row.Telephone_2 = parsedNumber;
etc...
It's a bit of a pain to replicate 5 times so you could convert it to a routine instead but since you only ever have at most two values as a result of the split, I would probably just use expressions like:
row1.Telephone_1.replaceAll("ext?\\d+","") for the phone number
row1.Telephone_1.replaceAll("^\\d+(ext?)?","") for the extension, noting that this will be "" not null if there's no extension
One Star

Re: How to use String.split function

Hi, Folks.
I have a similar problem using Talend for Big Data v.5.6.1.
I have one file (for example: csv) with a duplicate ids, but others data diference.
The first step I have to do is to aggregate all records duplicate in only one record based on the id key.
id | no_unid_forn | no_uni_forn  | capacidade | sg_unid_medida
1  | item1           | CARTELA      | 10,00        |
1  | item1           | UNIDADE     | 1,00          |  UN
1  | item1           | CAIXA         | 7,00          |  CJ
2  | item2           | FRASCO       | 500,00       | ML
2  | item2           | UNIDADE     | 750,00       | UN
2  | item2           | LITRO          |  1000,00    | L
2  | item2           | CAIXA          | 12,00        | CJ
And I do this using the component tDenormalize and get the following result.
id | no_unid_forn | no_uni_forn                                    | capacidade                               | sg_unid_medida
1  | item1           | CARTELA ; UNIDADE ; CAIXA            | 10,00 ; 1,00 ; 7,00                     |   ; UN ; CX
2  | item2           | FRASCO ; UNIDADE ; LITRO ; CAIXA  | 500,00 ; 750,00 ; 1000,00 ; 12,00 | ML ; UN ; L ; CX
So, now I need to create a new_field with the composition of these fields (no_unid_forn, capacidade, sg_unid_medida) in order, like that:
id | no_unid_forn | new_field
1  | item1           | CARTELA (10,00 - ) ; UNIDADE (1,00 - UN) ; CAIXA (7,00 - CX)
2  | item2           | FRASCO (500,00 - ML) ; UNIDADE (750,00 - UN) ; LITRO (1000,00 - L) ; CAIXA (12,00 - CX)
How can I concatenate these fields (no_unid_forn, capacidade, sg_unid_medida) in order of occurrence?