cancel
Showing results for 
Search instead for 
Did you mean: 

Last 8 positions of the string

Former Member
0 Kudos

Hi,

I need to do get only the last 8 charters of the string, how can I do this? please help

Eg:

Input

1234567890

output

34567890

or

Input

1234567890456

output

67890456

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Just write this simple UDF:

This UDF takes a single String array as input(say s[ ]) and it has a single ResultList item as output(say result)

CODE IS AS FOLLOWS:

String k=null;
k=s[0].substring((s[0].length()-8),(s[0].length()));

result.addValue(k);

Pass the output of this UDF to the target field. That's it. I have tested this code. It was successful.

Thanks

Biswajit

Edited by: 007biswa on Feb 16, 2011 1:15 PM

Edited by: 007biswa on Feb 16, 2011 1:15 PM

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

you have two standard functions:

1 substring(start position, size) -> return a "size" length substring from your start position

2 length -> return the size of a string

So you wan really substrin(length-7, 😎


             +--------+     +-----+
inputTag --> | length | --> |     |     +-----------+
             +--------+     | add | --> |           |
                      7 --> |     |     |           |
                            +-----+     | substring |  --> outputTag
                                        |           |
                                  8 --> |           |
                                        +-----------+

Let me know if you need more details.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Hi

Tested this simple udf and works as per the need

Title: lastEightCharacters

Execution type: Single values

public String lastEightCharacters(String var1, Container container) throws StreamTransformationException{

int length = var1.length();

String str = var1.substring(length-8,length);

return str;

}

Edited by: Baskar Gopal on Feb 15, 2011 4:54 PM

Former Member
0 Kudos

Try use this UDF, where the parameters sInput contains values like 1234567890 and sNoOfChars has number of retunr characters.

Argument:

sInput

sNoOfChars

=============

int iStartIndex = 0;

int iNoOfChars = Integer.parseInt(sNoOfChars);

iStartIndex = sInput.length() - iNoOfChars;

iStartIndex = ((iStartIndex < 0) ? 0 : iStartIndex);

return sInput.substring(iStartIndex);

Regards

Ramg

Former Member
0 Kudos

I am passing only one input value here, why do we need 2 arguments?

for eg:

Input

1234567890

output

34567890

Former Member
0 Kudos

By using 2 arguments in the mapping, we can Re-use the UDF for other interfaces.

Regards

Ramg