删除java代码中的前导零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25374618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:15:05  来源:igfitidea点击:

Removing leading zero in java code

java

提问by user3835327

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

我可以知道如何删除 JAVA 代码中的前导零吗?我尝试了几种方法,例如正则表达式工具

"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.

但似乎不支持我当前的编译器合规性级别 (1.3),会有一条红线表明方法 replaceFirst(String,String) 未定义为 String 类型。

Part of My Java code

我的 Java 代码的一部分

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.

感谢帮助。

采纳答案by Elliott Frisch

If you want remove leading zeros, you could parse to an Integerand convert back to a Stringwith one line like

如果你想删除前导零,你可以解析为一个Integer并转换回String一个像

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

int s = Integer.parseInt(seat);
System.out.println(s);   

回答by paxdiablo

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

replaceFirst()是在 1.4 中引入的,而您的编译器早于它。

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 only0characters.

两者都处理空字符串和包含0字符的字符串的退化情况。

回答by jondinham

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)

如 Java 文档中所述,“replaceFirst”仅从 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;
}

回答by ps2090

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

使用 java 方法str.replaceAll("^0+(?!$)", "")会很简单;

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

First parameter:regex -- 与此字符串匹配的正则表达式。

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

Second parameter:replacement -- 将替换匹配表达式的字符串。