Monday, September 05, 2005

ASP.NET TextBox, Trim White Spaces within words

ASP.NET TextBox provides a lot of features which reduces many a plumbing work required in previous versions of HTML Input Type.

Some of the built-in functionalities of <asp:textbox> such as TextMode to specify whether it is a Text field, a password field or Text Area simplify the programming task and provide meaningful interfaces to work with.

One of the important tasks we do when receiving user input in TextBox is to Trim the white spaces entered by the users. This can very well minimize the size of the data as well as storage in DataStores.

Some of the built-in methods that ship with the string class are

string.TrimStart()

Removes the specified set of occurances from the Beginning of the string

string.TrimEnd()

Removes the specified set of occurances from the End of the string

string.Trim()

Removes White Spaces from Beginning & End of the string. Very useful in applications while retrieving user input to remove the white spaces.

To utilize the above functions with TextBox input entered by the users, we can simply pass TextBox1.Text.Trim(). Since TextBox1.Text returns a string of the text entered by the user.

However, if we would like to trim the extra white spaces entered by the users in the TextBox in the form of a sentence etc., then the above methods would'nt be useful.

Say the user has entered "This       is     a     Word". We would like to save it/process it as "This is a Word", with the extra white spaces trimmed off.

To do that, we can use RegularExpressions. The following code demonstrates how we can remove extra white spaces recursively between words.

using System.Text.RegularExpressions;

private string RemoveSpaces(string str)
{
string result = "";
Regex regulEx = new Regex(@"[\s]+");
result = regulEx.Replace(str," ");
return result;
}


The above function receives a string and trims all the occurances of extra white spaces between words.

So, an input of "This       is     a     Word" will return the output as "This is a Word"

Thanks

8 comments:

carter84jennifer said...

Just passing by your blog and though you'd like this site.

michelingals50951422 said...

i thought your blog was cool and i think you may like this cool Website. now just Click Here

hexadecimals resources said...

I was looking for a place to post my blogs article financial in issue reporting segmental statement and stumbled across your blog. I found a cool site to post my link to my blog which is free and if you would like you can add your link aswell. It helps with getting traffic. Anyway, nice blog...I read posts with my coffee every morning. Ok then, have a great day : )

article financial in issue reporting segmental statement

vente said...

Vous avez un blog très agréable et je l'aime, je vais placer un lien de retour à lui dans un de mon blogs qui égale votre contenu. Il peut prendre quelques jours mais je ferai besure pour poster un nouveau commentaire avec le lien arrière.

Merci pour est un bon blogger.

Greg said...

Nice Blog!!! It looks like you have spent a fair amount of time setting it up and keeping the content fresh. Congratulations. I'll be sure to come back later again.

I have a SEO tips site. It pretty much covers keywords phrases
related stuff.

Thanks again and keep up the good work.

Greg
theseo-rank.com

Greg said...

This blog is great! If you get a chance you may want to visit this seeding-submission
site, it's

pretty awesome, too!

Greg said...

This blog is great! If you get a chance you may want to visit this relationship search engine
site, it's

pretty awesome, too!

dotnetty guy said...

hi your blog is very good and having helpful information and also good information it consists..really it awsome.