cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to remove leading zeros ,trailing zeros and in between spaces of string

manemahesh
Participant
0 Kudos

Hi All,

Can you please help me with UDF to remove leading zeros,trailing zeros and in between spaces of string.

e.g input string:-0012A45 89 4500

output string:-12A458945

Please help

Regards

Mahesh

Accepted Solutions (1)

Accepted Solutions (1)

manemahesh
Participant
0 Kudos

Thank you Anoop.

Answers (5)

Answers (5)

former_member207703
Active Participant

Hi,

These bunch of java codes must be suite your requirement:-

String output  = input.replaceAll(" ", "");
output = output.trim().replaceFirst("^0+(?!$)", "");
output = new StringBuffer(output).reverse().toString();
output = output.trim().replaceFirst("^0+(?!$)", "");
output = new StringBuffer(output).reverse().toString();
return output;

Regards,

Anoop Kumar Rai

Andrzej_Filusz
Contributor

Hi Mahesh,

If you want to remove leading and trailing zeros (and save any zeros inside your string) then your code could be like below:

// remove leading zeros
while(input.startsWith("0")) { 
  input = input.substring(1, input.length()); 
} 
// remove trailing zeros
while(input.endsWith("0")) { 
  input = input.substring(0, input.length() - 1); 
} 
// remove spaces
input = input.replaceAll("\\s","");
return input;

Regards,

Andrzej Filusz

former_member186851
Active Contributor

Hello Mahesh,

Try the below UDF.

String output = input.replaceAll("0", "");
String out=output.replaceAll("\\s","");
return out;

manemahesh
Participant
0 Kudos

Thank you Anoop.

former_member186851
Active Contributor
0 Kudos

Replace all will replace all zeros.:)