on 06-07-2007 10:54 AM
hi,
My task is that i ave to Trim the left or right most occurances of a Character.For exp
my input = 003045609800
than if the characters to be trimmed is '0' than on left & right trim the output should appear as below
lefttrim=3045609800
righttrim=003045609800
Please help me with the Code.
Thanks in Advance,
Bhargav
Hi Bhargav
for alphanumeric you can write UDF that accepts input string and returns string
for right trim
take maxLength suppose 12, length of source string(can be alphanumeric) suppose strLen
then you can write as
for(i=0; i< (12 - strLen); i++) {
Str1 = "0"+Str1;
}
This code will insert leading zeros
For left trim
for (i=0;i<(12-strLen);i++) {
Str1 = Str1+"0";
}
and return the resultant string..
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ,
1) Lets assume that maxLength is 12 then for righttrim you can use formatNum function(graphical mapping) from Arithmatic functions group
use it as
sorce -> formatNum(give 12 zeros in properties)-> target
2) for left trim if you want to remove leading zeros, for that just multiply source with constant "1" for example 0010 mul(1) will give 10.
Hope this helps.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
This should be the code that you are looking for
/**
Left trim.
@param pstrValue The string to left trim
@return Returns a left trimmed string
*/
public static String ltrim(String pstrValue) {
if (StringUtil.len(pstrValue) > 0) {
if (pstrValue.charAt(0) == ' ') {
if (StringUtil.len(pstrValue.trim()) == 0) {
return ""; // This is an empty string
} else {
// Remove the fist char if it is a space
while (pstrValue.charAt(0) == ' ') {
pstrValue = pstrValue.substring(1);
}
return pstrValue;
}
} else {
return pstrValue;
}
} else {
return pstrValue;
}
}
/**
Right trim.
@param pstrValue The value to right trim
@return Returns a right trimmed string
*/
public static String rtrim(String pstrValue) {
if (StringUtil.len(pstrValue) > 0) {
if (pstrValue.charAt(pstrValue.length()) == ' ') {
if (StringUtil.len(pstrValue.trim()) == 0) {
return ""; // This is an empty string
} else {
// Remove the fist char if it is a space
while (pstrValue.charAt(pstrValue.length()) == ' ') {
pstrValue = pstrValue.substring(pstrValue.length() - 1);
}
return pstrValue;
}
} else {
return pstrValue;
}
} else {
return pstrValue;
}
}
regards
krishna
If your field will contain only numbers, then it is simple. Use "formatNum" to achieve this. If the field will contain alpha numerica values, you need an UDF
This removes leading 0s
return (a.replaceFirst("^0+",""));
Regards,
Jai Shankar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
if your input is
//4564365600
static String leftTRim(String str, String pattern) {
int s = 0;
int e = 0;
StringBuffer result = new StringBuffer();
while ((e = str.indexOf(pattern, s)) >= 0) {
result.append(str.substring(e, string.length));
}
return result.toString();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
String lefttrim = substring( 2,input.length)
String rightrim = input+"00";
regards
krishna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey i think u didnt get my requirement
the input i will get is the only the character that in this case is '0'.
but i have no information as to how many times it occurs on the left or right side.As u said if i take
String lefttrim = substring( 2,input.length)
this works fine for the i/p 0038048900
but if the i/p is 000038048900
than it removes only the first two characters right
my task is if it is lefttrim
than is i/p is either 0038048900 or 000038048900 the output should be
38048900
User | Count |
---|---|
82 | |
10 | |
10 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.