Skip to main content
Announcements
Global Transformation Awards! Applications are now open. Submit Entry
cancel
Showing results for 
Search instead for 
Did you mean: 
rp2018
Creator
Creator

How to extract a word in a string using tjava component?

How to extract a word before records in a string?

String1: Test md records.

String2: 123 primary total records to count.

String3: Test records in file 123records.txt.

Extract as follow:

String1: md

String2: total

String3: test

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable

Hi

Assuming these string value comes from a data source, try this expression on tJavaRow:

output_row.columnName=((input_row.columnName.substring(0, input_row.columnName.indexOf("records"))).trim()).substring(((input_row.columnName.substring(0, input_row.columnName.indexOf("records"))).trim()).lastIndexOf(" "));

 

Let me know if you it works?

 

Regards

Shong

View solution in original post

3 Replies
Anonymous
Not applicable

Hi

Assuming these string value comes from a data source, try this expression on tJavaRow:

output_row.columnName=((input_row.columnName.substring(0, input_row.columnName.indexOf("records"))).trim()).substring(((input_row.columnName.substring(0, input_row.columnName.indexOf("records"))).trim()).lastIndexOf(" "));

 

Let me know if you it works?

 

Regards

Shong

Mahamoutou
Contributor III
Contributor III

You could also use regex expression with java replaceAll function like below :

 

YourInputString.replaceAll( "^(.*?)([A-Za-z]+)(\\s*)(records)(.*?)$", "$2" )

 

 0695b00000LxFMxAAN.png 

0695b00000LxFKhAAN.png

rp2018
Creator
Creator
Author

Thanks this solution worked and only thing I needed to do is to add a replaceall to trim the spaces coming before the output word.

 

output_row.columnName=((input_row.columnName.substring(0, input_row.columnName.indexOf("records"))).trim()).substring(((input_row.columnName.substring(0, input_row.columnName.indexOf("records"))).trim()).lastIndexOf(" ")).replaceAll(" ", "");