on 04-26-2013 7:17 PM
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.
Thank you so much .. .. now its working fine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.