Skip to end of metadata
Go to start of metadata

Code for removing leading zeros from a string

public java.lang.String removeLeadingZeros( java.lang.String str ){
if (str == null){
return null;}
char[] chars = str.toCharArray();
int index = 0;
for (; index < str.length();index++)
{
if (chars[index] != '0'){
break;}
}
return (index == 0) ? str :str.substring(index);
}

Code for removing trailing zeros from a string


public java.lang.String removeTrailingZeros( java.lang.String str ){
if (str == null){
return null;}
char[] chars = str.toCharArray();int length,index ;length = str.length();
index = length -1;
for (; index >=0;index--)
{
if (chars[index] != '0'){
break;}
}
return (index == length-1) ? str :str.substring(0,index+1);
}

1 Comment

  1. Anonymous

    you don't need to write "java.lang.String". "java.lang.*" is imported automatically. you just need to write "String".