English 中文(简体)
如何从字母数字中删除主要零?
原标题:How to remove leading zeros from alphanumeric text?

我看到了如何在SO中预先确定零的问题。 但这不是另一种方式!

您能向我提出如何删除字母数字中的主要零点? 是否有任何内装的促动器,或者我是否需要书写一种方法,将头零碎打碎?

例:

01234 converts to 1234
0001234a converts to 1234a
001234-a converts to 1234-a
101234 remains as 101234
2509398 remains as 2509398
123z remains as 123z
000002829839 converts to 2829839
最佳回答

Regex是这项工作的最佳工具;它应当取决于问题的具体说明。 下面删除了铅零,但在必要时留下一个(即它只剩下<代码>0”到空白处。

s.replaceFirst("^0+(?!$)", "")

www.un.org/Depts/DGACM/index_french.htm 封面将确保在投入开始时匹配的<代码>0+。 <代码>(!$) 负光头确保不与整条镜子相匹配。

试验:

String[] in = {
    "01234",         // "[1234]"
    "0001234a",      // "[1234a]"
    "101234",        // "[101234]"
    "000002829839",  // "[2829839]"
    "0",             // "[0]"
    "0000000",       // "[0]"
    "0000009",       // "[9]"
    "000000z",       // "[z]"
    "000000.z",      // "[.z]"
};
for (String s : in) {
    System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]");
}

See also

问题回答

如果你使用科特林 这是你需要的唯一法典:

yourString.trimStart( 0 )

如何管理:

String s = "001234-a";
s = s.replaceFirst ("^0*", "");

<代码><>>>>> 指示数在显示开始时(如果从背景来看,你的指示数不是多线,否则,你可能需要在输入时而不是从线开始时查阅<代码>A。 。 <代码>replaceFirst 仅以无所作为取代所有<代码>0。

如果像Vadzim一样,你关于主要零点的定义包括将0”(或000”或类似的扼杀物变为空洞(合理地期望),那么只要有必要,你就将它重新定位:

String s = "00000000";
s = s.replaceFirst ("^0*", "");
if (s.isEmpty()) s = "0";

明确无误地无需登记和任何外部图书馆。

public static String trimLeadingZeros(String source) {
    for (int i = 0; i < source.length(); ++i) {
        char c = source.charAt(i);
        if (c !=  0 ) {
            return source.substring(i);
        }
    }
    return ""; // or return "0";
}

You could just do: String s = Integer.valueOf("0001007").toString();

使用:

String x = "00123".replaceAll("^0*", ""); // -> 123

利用团体登记:

Pattern pattern = Pattern.compile("(0*)(.*)");
String result = "";
Matcher matcher = pattern.matcher(content);
if (matcher.matches())
{
      // first group contains 0, second group the remaining characters
      // 000abcd - > 000, abcd
      result = matcher.group(2);
}

return result;

正如一些答复所显示的那样,利用reg是这样做的好办法。 如果你不希望使用该条例,你可以使用该守则:

String s = "00a0a121";

while(s.length()>0 && s.charAt(0)== 0 )
{
   s = s.substring(1); 
}

如果你(与我一样)需要从每一条“字句”中删除所有主要的零,那么你可以修改对以下各点的@polygenelubricants的答复:

String s = "003 d0g 00ss 00 0 00";
s.replaceAll("\b0+(?!\b)", "");

结果是:

3 d0g ss 0 0 0

使用lin子很容易

value.trimStart( 0 )

我认为,这样做非常容易。 你们只能从一开始就坐下来,从零起,直到你找到了一点点。

int lastLeadZeroIndex = 0;
for (int i = 0; i < str.length(); i++) {
  char c = str.charAt(i);
  if (c ==  0 ) {
    lastLeadZeroIndex = i;
  } else {
    break;
  }
}

str = str.subString(lastLeadZeroIndex+1, str.length());

未使用<代码>Regex 或substring(>,String,其效率将不高,

public static String removeZero(String str){
        StringBuffer sb = new StringBuffer(str);
        while (sb.length()>1 && sb.charAt(0) ==  0 )
            sb.deleteCharAt(0);
        return sb.toString();  // return in String
    }

页: 1

       String s="0000000000046457657772752256266542=56256010000085100000";      
    String removeString="";

    for(int i =0;i<s.length();i++){
      if(s.charAt(i)== 0 )
        removeString=removeString+"0";
      else 
        break;
    }

    System.out.println("original string - "+s);

    System.out.println("after removing 0 s -"+s.replaceFirst(removeString,""));

If you don t want to use regex or external library. You can do with "for":

String input="0000008008451"
String output = input.trim();
for( ;output.length() > 1 && output.charAt(0) ==  0 ; output = output.substring(1));

System.out.println(output);//8008451

我进行了一些基准测试,发现这种解决办法(迄今为止)最快:

    private static String removeLeadingZeros(String s) {
      try {
          Integer intVal = Integer.parseInt(s);
          s = intVal.toString();
      } catch (Exception ex) {
          // whatever
      }
      return s;
    }

更长时间内,特别经常的言论非常缓慢。 (我需要找到最快的办法来打捞)

什么是仅仅寻找第一个非零性质?

[1-9]d+

This regex finds the first digit between 1 and 9 followed by any number of digits, so for "00012345" it returns "12345". It can be easily adapted for alphanumeric strings.

  const removeFirstZero = (ele) => parseInt(ele).toString()
  
  console.log( raw   +  0776211121 )
  console.log( removedZero   + removeFirstZero( 0776211121 ))




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签