简体   繁体   中英

Removing leading zero in java code

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.

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);   

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.

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM