Nitin Jaiman
Nitin Jaiman

Reputation: 173

How can one extract number of trailing zeros from String

I want to extract trailing zeros from a string for eg //8320987112741390144276341183223364380754172606361245952449277696409600000000000000 should yield 14 my approach was to first find the length of above string then subtract it by length of the stripped trailing zero string. I tried to find the later using BigDecimal stripTrailingZeros() method but it is only removing zeros after decimal

for eg

1200.000 is converted to 1200 by stripTrailingZeros() method but i want 12 as output

any idea how to solve this problem?

Upvotes: 3

Views: 1481

Answers (5)

DuncanKinnear
DuncanKinnear

Reputation: 4643

OK, expanding on Jon's answer, and assuming that you don't want to count the decimal point, then the following code should cope with both integers and decimal numbers in your string:

// Start by taking note of the original string length
int startLength = text.length();
// Remove any trailing zeroes
text = text.replaceAll("0*$", "");
// Remove any zeroes immediately to the left of any trailing decimal point
text = text.replaceAll("0*.$", ".");
// Find the number of zeroes by subtracting the lengths
int numZeroes = startLength - text.length();

or if you want it in a single statement:

int numZeroes = text.length() - text.replaceAll("0*$", "").replaceAll("0*.$", ".").length();

Which looks horrible, but has the advantage of not altering the original string.

Upvotes: 0

Sahil Jain
Sahil Jain

Reputation: 187

Does this answer fulfills your requirement?

String str = "8320987112741390144276341183223364380754172606361245952449277696409600000000000000";
    String withoutTrailingZeroes = "";
    for (int i = str.length() - 1; i >= 0; i--) {
        String charFromString = Character.toString(str.charAt(i));
        if (charFromString.equals("0")) {
            withoutTrailingZeroes += str.charAt(i);
        } else {
            break;
        }
    }
    System.out.println(str);
    System.out.println(str.replace(withoutTrailingZeroes, "")); // string without trailing zeros
    System.out.println(withoutTrailingZeroes); // trailing zeroes
    System.out.println(withoutTrailingZeroes.length()); // trailing zeroes length

Upvotes: 0

Martin Frank
Martin Frank

Reputation: 3454

well - this is simple but a compute-intesiv solution...

String str = "8320987112741390144276341183223364380754172606361245952449277696409600000000000000";
System.out.println(str);
int count = 0;
char c = '0';
do {
    c = str.charAt(str.length()-1);
    if (c == '0' ){
        str = str.substring(0, str.length() - 1);
        count ++;
    }
}while(c == '0' );
System.out.println(str);
System.out.println("amount:"+amount);

but it's a very obvious solution... (just remove last zero...until there is no more...)

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

If you want the length of trailing zeroes, you could do this regex :

public static void main(String[] args) {
    String s = "8320987112741390144276341183223364380754172606361245952449277696409600000000000000";
    Pattern p = Pattern.compile("0+$");
    Matcher m = p.matcher(s);
    m.find();
    String val = m.group();
    System.out.println(val);
    System.out.println(val.length());

}

O/P :

00000000000000
14

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500425

The simplest option would probably be to use String.replaceAll:

text = text.replaceAll("[0.]*$", "");

The $ makes sure it's only trimming the end of the string.

Note that if you start with "0" you'll end up with an empty string - think about what you want the result to be in that situation.

Upvotes: 3

Related Questions