Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Phagu Mahato
More...


Resources » Code Snippets » Regular Expressions

Remove Trailing Zeroes From a String in C# using Regular Expressions


Posted Date:     Category: Regular Expressions    
Author: Member Level: Bronze    Points: 10



 


The code sample is that of a function RemoveTrailingZeros in C#, that takes a string input and removes all trailing zeros in it.

The code uses the Regex object provided by the .Net class library. Regular Expressions optimize the operation of "searching patterns" in a string. The following code can also be taken as an example to remove any character (trailing) from a string. The only change require will be that we need to change the Regular Expression.


//Regex is in System.Text.RegularExpressions namespace
public string RemoveTrailingZeroes(string input)
{
Regex reg1 = new Regex("\\.\\d*(?<1>0+)[^\\d]*$", RegexOptions.IgnoreCase);

Match m = reg1.Match(input);
if( m.Success )
{
input = input.Replace(m.Groups["1"].Value, "");
Regex reg2 = new Regex("(?<1>\\.)[^\\d]*$", RegexOptions.IgnoreCase);
m = reg2.Match(input);

if( m.Success )
{
input = input.Replace(".", "" );
}
Regex reg3 = new Regex("\\d", RegexOptions.IgnoreCase );
m = reg3.Match(input);
if( !m.Success )
{
input = "0" + input;

}

}
if(input.StartsWith("."))
input = "0" + input;

return input;
}





Did you like this resource? Share it with your friends and show your love!


Responses to "Remove Trailing Zeroes From a String in C# using Regular Expressions"
Author: Dan    08 May 2009Member Level: Bronze   Points : 1
This might be easier and faster:

public string RemoveTrailingZeroes(string s)
{
s = s.TrimStart('0').TrimEnd('0', '.');
if(s.StartsWith(".")) s = "0" + s;
return s;
}



Guest Author: Pitrs81     03 Jan 2012
Not even one method works correctly.

First (using regex) doesn't work for numbers like 1.1010 (output is 1.11).
Second (using trim) doesn't work for numbers like 7000.00 (output is 7).



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Text validation for TextInput in Flex
    Previous Resource: To restrict date to mm/dd/yyyy format
    Return to Resources
    Post New Resource
    Category: Regular Expressions


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Remove trailing zeroes using regular expressions  .  Regular expressions in .net  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.