3

I am trying to remove all the leading zero from the number part of a string. I have came up with this code (below). From the given example it worked. But when I add a '0' in the begining it will not give the proper output. Anybody know how to achive this? Thanks in advance

input: (2016)abc00701def00019z -> output: (2016)abc701def19z -> resut: correct

input: 0(2016)abc00701def00019z -> output: (2016)abc71def19z -> result: wrong -> expected output: (2016)abc701def19z

EDIT: The string can contain other than english alphabet.

String localReference = "(2016)abc00701def00019z";
String localReference1 = localReference.replaceAll("[^0-9]+", " ");
List<String> lists =  Arrays.asList(localReference1.trim().split(" "));
System.out.println(lists.toString());
String[] replacedString = new String[5];
String[] searchedString = new String[5];
int counter = 0;
for (String list : lists) {
   String s = CharMatcher.is('0').trimLeadingFrom(list);
   replacedString[counter] = s;
   searchedString[counter++] = list;

   System.out.println(String.format("Search: %s, replace: %s", list,s));
}
System.out.println(StringUtils.replaceEach(localReference, searchedString, replacedString));
Deb
  • 2,922
  • 1
  • 16
  • 32
  • i bet there is a regex for it, but you could also take all the numbers out and convert them to an int. this will remove all leading zeros. you then only have to replace the previous values with the new ones – XtremeBaumer Feb 21 '17 at 07:37
  • @XtremeBaumer But that would fail for very large number sequences that are too large for int to hold – Sentry Feb 21 '17 at 07:38
  • @Sentry not sure, but maybe BigInteger also removes leading zeros. and i doubt you will have a string containing a number to overload a bigInteger – XtremeBaumer Feb 21 '17 at 07:44
  • Possible duplicate of [How to remove leading zeros from alphanumeric text?](http://stackoverflow.com/questions/2800739/how-to-remove-leading-zeros-from-alphanumeric-text) Does this help??? – Chetan Kinger Feb 21 '17 at 07:45
  • @CKing not really. it only removes the very first zero, but not the ones in the middle of the string. you would have to do 2 replacements, but regex should work with 1 replacement – XtremeBaumer Feb 21 '17 at 07:48
  • @CKing That's a different question. In that question the string will only contain numbers. In this I have to find all the number part from the string and remove leading 0 from it and re build it with the charachter and the number part (without leading 0). – Deb Feb 21 '17 at 07:48
  • @XtremeBaumer Thanks. I will try once with the regular expression – Deb Feb 21 '17 at 07:50

3 Answers3

3
str.replaceAll("(^|[^0-9])0+", "$1");

This removes any row of zeroes after non-digit characters and at the beginning of the string.

Markus Benko
  • 1,507
  • 8
  • 8
0

java has \P{Alpha}+, which matches any non-alphabetic character and then removing the the starting Zero's.

String stringToSearch = "0(2016)abc00701def00019z"; 
Pattern p1 = Pattern.compile("\\P{Alpha}+");
Matcher m = p1.matcher(stringToSearch);
StringBuffer sb = new StringBuffer();
while(m.find()){
    m.appendReplacement(sb,m.group().replaceAll("\\b0+",""));
}
m.appendTail(sb);
System.out.println(sb.toString());

output:

(2016)abc701def19z
Avaneesh
  • 162
  • 5
  • Thanks for your reply. But this will fail if the string contain other than english alphabet. – Deb Feb 21 '17 at 08:15
0

I tried doing the task using Regex and was able to do the required according to the two test cases you gave. Also $1 and $2 in the code below are the parts in the () brackets in preceding Regex.

Please find the code below:

    public class Demo {

        public static void main(String[] args) {

            String str = "0(2016)abc00701def00019z";

/*Below line replaces all 0's which come after any a-z or A-Z and which have any number after them from 1-9. */
            str = str.replaceAll("([a-zA-Z]+)0+([1-9]+)", "$1$2");
            //Below line only replace the 0's coming in the start of the string
            str = str.replaceAll("^0+","");
            System.out.println(str);
        }
    }
Mohit
  • 608
  • 4
  • 19