C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






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


Posted Date: 20 Jun 2008    Resource Type: Code Snippets    Category: Regular Expressions
Author: Amit GuptaMember Level: Silver    
Rating: 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;
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Remove trailing zeroes using regular expressions  .  Remove trailing zeroes in c#  .  Remove trailing zeroes from a string in c#  .  Regular expressions in .net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Text validation for TextInput in Flex
Previous Resource: To restrict date to mm/dd/yyyy format
Return to Discussion Resource Index
Post New Resource
Category: Regular Expressions


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use