Apex Code Sample Illustrating How to Drop Leading Zeros

Here is an example regarding the use of the pattern and matcher methods in an Apex class. For this particular code sample I am using the methods to check a passed text string for the occurrence of leading zeros in the string and removing them when they exist. The code I have written here was actually used on a project where I had to validate that User records contained a valid EmployeeNumber value. And, as you probably know, this particular field is editable by users and is stored as text in the database. Rather than go through the trouble of creating another Number type field on the User object I thought it would be easier and less disruptive to simply check the value when I needed to reference it in any Apex logic.

For the purposes of this post I created a helper class that can be referenced from another Apex class. The entire code including test class with 100% code coverage is listed below.

/*
    Created by: Greg Hacic
    Last Update: 14 June 2011 by Greg Hacic
    Questions?: greg@interactiveties.com
*/
public class useful_code {
    
    //removes leading zeros from a passed string of numbers or returns NULL when not all numeric characters
    public static String drop_leading_zeros(String passedValue) {
        String return_string = null; //return string for passing back
        if (passedValue != null) { //if the passed value is not null
            return_string = passedValue.trim(); //trim the whitespace from the start and end of the value
            Pattern valid_characters = Pattern.compile('([0-9]+)'); //only numbers
            Matcher check_chars = valid_characters.matcher(return_string); //compare the string to the set of valid characters
            if (check_chars.matches()) { //if we have a somewhat valid number
                if (return_string.startsWith('0') && return_string.length() > 1) { //if the string begins with a 0 and the length is greater than 1
                    boolean keepChecking = true; //create a boolean variable
                    while (keepChecking) { //if boolean is true
                        if (return_string.startsWith('0') && return_string.length() > 1) { //if the string begins with 0 and there is more than 1 character
                            return_string = return_string.substring(1); //drop the first character
                        } else { //either the string doesn't begin with 0 or the length is less than or equal to 1
                            keepChecking = false; //stop the loop
                        }
                    }
                }
                if (return_string == '0') { //if the resulting string is now a single '0'
                    return_string = null; //set the string to null
                }
            } else { //otherwise the value passed was not valid
                return_string = null; //set the string to null
            }
        }
        return return_string; //pass back a value
    }
    
	@isTest //defines method for use during testing only
    static void test_drop_leading_zeros() {
        System.assertEquals('11111', drop_leading_zeros('11111'));
        System.assertEquals('11111', drop_leading_zeros('00000011111'));
        System.assertEquals(null, drop_leading_zeros('abc'));
        System.assertEquals('99999', drop_leading_zeros(' 99999 '));
        System.assertEquals('999', drop_leading_zeros(' 00999 '));
        System.assertEquals(null, drop_leading_zeros('w'));
        System.assertEquals(null, drop_leading_zeros('00'));
        System.assertEquals(null, drop_leading_zeros('(303) 317-2235'));
        System.assertEquals(null, drop_leading_zeros('111 091'));
        System.assertEquals(null, drop_leading_zeros('  '));
        System.assertEquals(null, drop_leading_zeros(''));
        System.assertEquals(null, drop_leading_zeros(null));
    }
}

The comments in the code should provide some relevant business logic regarding what I'm trying to accomplish within each line of code. At a high level I basically take a string, which is passed to the class when it is called via method, and check the first character. The check validates that the character is a number. If it is a zero and the first character in the string, I drop the character and check the remaining characters again. While iterating over each character I not only drop the leading zeros but I also validate that the character is a valid number.

Automated Exchange Rates in Salesforce.com

Reduce Repetitive Tasks, Eliminate Errors & Free Up Your Administrators.

Birthday Reminders for Salesforce.com

It might lead to a sale. Or it might make you feel good.