Skip to Content
avatar image
Former Member

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Jun 13, 2017 at 09:02 AM

    Thank you Anoop.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 12, 2017 at 05:59 AM

    Hello Mahesh,

    Try the below UDF.

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

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 12, 2017 at 04:11 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 13, 2017 at 03:34 AM

    Replace all will replace all zeros.:)

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 13, 2017 at 02:09 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 13, 2017 at 09:04 AM

    Thank you Anoop.

    Add comment
    10|10000 characters needed characters exceeded