cancel
Showing results for 
Search instead for 
Did you mean: 

substring to get last values

former_member217945
Participant
0 Kudos

Dear Experts,

I am trying to get last 40 characters from my input value.

I am using substring providing values as (starting position as 3 and number of characters as 40)

but if the value is less than 40 I am facing error.

Could you please suggest option or UDF code for the same.

Thanks,

suma.

Accepted Solutions (0)

Answers (4)

Answers (4)

former_member217945
Participant
0 Kudos

Thank you so much .. .. now its working fine

baskar_gopalakrishnan2
Active Contributor
0 Kudos

You can trim even to few lines....

public String returnLast40Chars(String str){

String returnStr =""

if(s!=null && !s.equals(""))  {
returnStr= s.substring(s.length()-40, s.length());

}

return returnStr;

}

anupam_ghosh2
Active Contributor
0 Kudos

Hi Baskar,

                 This code will throw IndexOutOfBoundsException in case the input string length is less than 40.

Please correct me in case I am wrong.

Regards

Anupam

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Yep, if the string length is less than 40, it will throw indexoutofboundexception. The above code is assuming that the input length is always 40 and above. Adding the check is better.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Suma,

                 You can try this UDF

 

public static String returnLastChars(String s)

{

  int l,n=40;

  if(s!=null && !s.equals(""))

  {

      l=s.length();

      if(l>=n)

      {

       s=s.substring(l-n,l);

      }

  }

 

  return s;

}

If the input string "s" as less number of charcaters than 40 then entire string is returned by the UDF.

Regards

Anupam

Message was edited by: Anupam Ghosh

Former Member
0 Kudos

Hi.

You can use and standar function. with length.

Field--:>Length :>grea

Or you can use and UDF like this

String sResult = "";

if (sValue.length >= 40)

    sValue = sValue.substring(0,40);

else

    sResult = sValue;

return sResult;

Regards

Lucho

anupam_ghosh2
Active Contributor
0 Kudos

Hi Lucho,

                 The UDF you wrote returns first 40 characters instead of last 40. The statement

if (sValue.length >= 40)  

     

    sValue = sValue.substring(0,40); 

would return first 40 characters.

I think the graphical mapping too will also produce first 40 characters instead of last 40 ones.

Please correct me if i am wrong.

Regards

Anupam

Former Member
0 Kudos

Hi , yeah your rigth..:) .. I don't know why I write the code for first 40. Maybe was the beer.

Please @Suma  use the below code , this must be solved your problem.

sValue = Your Variable.

           String sResult = "";

           if (sValue.length() >= 40 &&  sValue!= null)

                sResult = sValue.substring(sValue.length()-40);

            else

                sResult = sValue;

           return sResult;

Regards.

Lucho.