String 有trim()方法,是去掉字符串前后的小于'\u0020'的字符的trim 0 和左trim 0都要自己写左trim 0就把它转成数字再转回来也可以

解决方案 »

  1.   

    @see #zeroTrim(String)
    <<
    public class TestZeroTrim extends TestCase {
        public TestZeroTrim(String name) {super(name);}    /**
         * Left zero trim.
         * @param str integer source string.
         * @return integer string without left zero.
         * @throws NullPointerException If passing null.
         * @throws NumberFormatException If usage error.
         */
        public String zeroTrim(String str) {
            if(str==null) throw new NullPointerException();
            return Integer.toString(Integer.parseInt(str));
        }    public void testZeroTrim() {
            assertEquals("1", zeroTrim("01"));
            assertEquals("10", zeroTrim("10"));
        }
    }
    >>
      

  2.   

    0 trim不掉,应该用其他方法
      

  3.   

    先toString再getBytes 最后想怎么样就怎么样 呵呵
      

  4.   

    左trim掉0的方法:String void LeftTrimZero(String s)
     {
      while(s.charAt(0)=='0')
        s=s.replaceFirst("0","");
      return s;  
     }
      

  5.   

    不做成方法,直接实现的例子:
    //去掉左空格
    class ZeroTrim
    {
     public static void main(String[] args)
     {
      String s="00010.100";
      while(s.charAt(0)=='0')
        s=s.replaceFirst("0","");
      System.out.println(s);  
     }
    }
    //结果:
    //10.100
    //Press any key to continue...
      

  6.   

    trim()方法会自动去掉字符串前后中的空白字符的,这些空白字符包括:\t,\n,\f,\r以及空格
      

  7.   

    去掉左右0以及所有0还得自己写,当然有很多种方法实现,比如通过替换的方式:replace();字符串拼接的方式substring()+substring()
      

  8.   

    如果想把 0000.100去0后改为 0.100 而不是 .100 的话,我的例子改为:
    //去掉左空格
    class ZeroTrim
    {
     public static void main(String[] args)
     {
      String s="0000.100";
      while(s.charAt(0)=='0' && s.charAt(1)!='.')
        s=s.replaceFirst("0","");
      System.out.println(s);  
     }
    }
    //结果:
    //0.100
    //Press any key to continue...