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.

2
  • 3
    Totally out of curiosity, why Java 1.3? Is this on an embedded system or something? That is ooooold by Java standards. Commented Aug 19, 2014 at 2:14
  • Possible duplicate of How to remove leading zeros from alphanumeric text? Commented Oct 21, 2015 at 15:27

4 Answers 4

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);   
Sign up to request clarification or add additional context in comments.

1 Comment

Thousand of thanks to you @Elliot. This method is much easy and efficient.
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.

1 Comment

Nice, Both acceptable too !! Thanks for the solution !!
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.

Comments

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

1 Comment

it's the same like using replaceFirst("^0*",""), the whole string is gone in that case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.