4

May I know how can I remove the leading zero in JAVA code? I tried several methods like regex tools

"s.replaceFirst("^0+(?!$)", "") / replaceAll("^0*", "");` 

but it's seem like not support with my current compiler compliance level (1.3), will have a red line stated the method replaceFirst(String,String)is undefined for the type String.

Part of My Java code

public String proc_MODEL(Element recElement)
{
 String SEAT        = "";
    try
    {
        SEAT    = setNullToString(recElement.getChildText("SEAT")); // xml value =0000500

       if (SEAT.length()>0)  
       {
           SEAT = SEAT.replaceFirst("^0*", "");  //I need to remove leading zero to only 500 
       }
       catch (Exception e)
       {
           e.printStackTrace();
           return "501 Exception in proc_MODEL";
       }
    } 
}

Appreciate for help.

user3835327
  • 570
  • 1
  • 5
  • 19
  • 3
    Totally out of curiosity, why Java 1.3? Is this on an embedded system or something? That is _ooooold_ by Java standards. – yshavit Aug 19 '14 at 02:14
  • Possible duplicate of [How to remove leading zeros from alphanumeric text?](http://stackoverflow.com/questions/2800739/how-to-remove-leading-zeros-from-alphanumeric-text) – dARKpRINCE Oct 21 '15 at 15:27

4 Answers4

15

If you want remove leading zeros, you could parse to an Integer and convert back to a String with one line like

String seat = "001";// setNullToString(recElement.getChildText("SEAT"));
seat = Integer.valueOf(seat).toString();
System.out.println(seat);

Output is

1

Of course if you intend to use the value it's probably better to keep the int

int s = Integer.parseInt(seat);
System.out.println(s);   
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

replaceFirst() was introduced in 1.4 and your compiler pre-dates that.

One possibility is to use something like:

public class testprog {
    public static void main(String[] args) {
        String s = "0001000";
        while ((s.length() > 1) && (s.charAt(0) == '0'))
            s = s.substring(1);
        System.out.println(s);
    }
}

It's not the most efficient code in the world but it'll get the job done.

A more efficient segment without unnecessary string creation could be:

public class testprog {
    public static void main(String[] args) {
        String s = "0001000";
        int pos = 0;
        int len = s.length();
        while ((pos < len-1) && (s.charAt(pos) == '0'))
            pos++;
        s = s.substring(pos);
        System.out.println(s);
    }
}

Both of those also handle the degenerate cases of an empty string and a string containing only 0 characters.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

Using a java method str.replaceAll("^0+(?!$)", "") would be simple;

First parameter:regex -- the regular expression to which this string is to be matched.

Second parameter: replacement -- the string which would replace matched expression.

ps2090
  • 223
  • 2
  • 15
0

As stated in Java documentation, 'replaceFirst' only started existing since Java 1.4 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceFirst(java.lang.String,%20java.lang.String)

Use this function instead:

String removeLeadingZeros(String str) {
  while (str.indexOf("0")==0)
    str = str.substring(1);
  return str;
}
jondinham
  • 8,271
  • 17
  • 80
  • 137