cancel
Showing results for 
Search instead for 
Did you mean: 

removing leading zeros

Former Member
0 Kudos

HI sap-xI guru,

I need to write a UDF to remove leading zeros.Can u please help me in writing it.

Please answer my question ASAP.

Thanks & Regards

Ambati

Accepted Solutions (1)

Accepted Solutions (1)

Hi

Please use format number function and use "#" in number format. It will remove the leading zeros.

Regards

Neeraj

Answers (6)

Answers (6)

deepak_shah
Contributor
0 Kudos

Hi here is uDF for your requirement. Say matno is your source field.

if (matno == null)

{

return null;

}

//Convert matno into character array

char[] chars = matno.toCharArray();

int index = 0;

//Loop through the array

for (; index < matno.length(); index++)

{

// If the character in the array is not '0' then break from the loopp

if (chars[index] != '0')

{

break;

}

}

//If the index is 0 then there is no leading 0 in the matno, return matno. Otherwise return the characters after the index where the 1st non 0 character was encountered

return (index == 0) ? matno : matno.substring(index);

- Deepak.

Former Member
0 Kudos

Subbu

Use standard function round.

Source field->round->target field

Thats it!!!

Why do you want to use UDF?

Regards

Soumen...

santhosh_kumarv
Active Contributor
0 Kudos

You dont require a UDF if the input field is a numeric field. With the graphical mapping, this can be done.

Just using the arithmetic function divide or multiply with the input field.

below will be the mapping

<input field> & <constant:1> -


> multiply -


> <target field>

eg..

000055687 * 1 = 55687.

000055687 / 1 = 55687.

note. This will not work if the input field in alphanumeric i.e. like 000058fg6697.

~SaNv...

Former Member
0 Kudos

No the input field is string value and every time no of leading zeros are varying.Sometime 4 zeros and sometime 3 zeros only.

santhosh_kumarv
Active Contributor
0 Kudos

Then Div or Mul will not work.. Use the below code as mentioned in [this wiki|http://wiki.sdn.sap.com/wiki/display/Java/RemoveLeadingandTrailingZerosfroma+String]

public java.lang.String removeLeadingZeros( java.lang.String str ){
if (str == null){
return null;}
char[] chars = str.toCharArray();
int index = 0;
for (; index < str.length();index++)
{
if (chars[index] != '0'){
break;}
}
return (index == 0) ? str :str.substring(index);
}

~SaNv...

Former Member
0 Kudos

Hi

Use FormatNumber function and pass Number Format Parameter as 0 (zero) and mapped this in between Source and Target Field.

or use following UDF,

String input;

try

{

input =String.valueOf( Integer.parseInt(a[0]));

result.addValue(input);

}

catch(Exception e)

{

try

{

result.addValue(a[0]);

}

catch(Exception ne)

{

}

}

Award the Points if information is useful.

Regards,

Nitin Patil

former_member204873
Contributor
0 Kudos

try to use:

String inp;

Integer x = new Integer(inp);

return x.toString();

Former Member
0 Kudos

Hi,

try below code...

("input" is the input parameter)

import java.util.regex*;

Lets say, String input = "00000000000000023202203203827";

String regex = "^0*";

String temp = input.replaceFirst(regex, "");

return temp;

Also check,

http://wiki.sdn.sap.com/wiki/display/Java/RemoveLeadingandTrailingZerosfroma+String

Regards,

Swetha.

former_member208856
Active Contributor
0 Kudos

Put this code in your UDF:

Name the function as "zerosuppress" and take the

cache as value

and take one argument input.

Then put the code below:

//write your code here

String output = input.replaceFirst("^0+","");

return output;

Former Member
0 Kudos

keep the code given by u is not working "String output = input.replaceFirst("^0+",""); "

but if i keep the code as "String output = input.replaceAll("^0+",""); " is working.

Can u plz tell me what could be the reason.

former_member208856
Active Contributor
0 Kudos

ReplaceFirst should work.

in Replace First & ReplaceAll there is one difference

ReplaceAll --> Replaces each substring of this string that matches.

ReplaceFirst --> Replaces the first substring of this string that matches.

Please check your UDF completely.

In my suggestion use ReplaceFirst.