Skip to Content
0

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

Jan 12, 2017 at 05:09 AM

509

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

6 Answers

Best Answer
MAHESH MANE Jun 13, 2017 at 09:02 AM
0

Thank you Anoop.

Share
10 |10000 characters needed characters left characters exceeded
Raghuraman S Jan 12, 2017 at 05:59 AM
1

Hello Mahesh,

Try the below UDF.

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


capture.png (1.6 kB)
Share
10 |10000 characters needed characters left characters exceeded
Andrzej Filusz Jan 12, 2017 at 04:11 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded
Raghuraman S Jan 13, 2017 at 03:34 AM
0

Replace all will replace all zeros.:)

Share
10 |10000 characters needed characters left characters exceeded
Anoop Rai Jan 13, 2017 at 02:09 PM
0

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


result.jpg (12.7 kB)
Share
10 |10000 characters needed characters left characters exceeded
MAHESH MANE Jun 13, 2017 at 09:04 AM
0

Thank you Anoop.

Share
10 |10000 characters needed characters left characters exceeded