cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to trim the left or right of a string

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Dear Mugdha,

I wanted the same for Alpha Numeric i/p

Thanks in Advance

Bhargav

Former Member
0 Kudos

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

Former Member
0 Kudos

In the above code , specify the character instead of " " in the condition for char[0].

regards

krishna

Former Member
0 Kudos

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

Former Member
0 Kudos

yes my field contains alpha numeric characters and if the character to be trimmed is not 0 than how to do.if it is character 'c' that has to be trommed for the i/p cc098nn99cc than can i use the same thing Jai

Former Member
0 Kudos

No. If you see the code, the character 0 is actually hardcoded there.

If you want, you can make this a little more dynamic. You can pass another input to this UDF, which will contain the character to be removed and then use that variable in the code instead of 0.

Regards,

Jai Shankar

Former Member
0 Kudos

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();

}

Former Member
0 Kudos

String lefttrim = substring( 2,input.length)

String rightrim = input+"00";

regards

krishna

Former Member
0 Kudos

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