Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Capitalize each word in a string

The CapitalizeString method uses regular expressions to capitalize every word in a string.

On Sunday, December 16th 2007 at 02:00 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.7 with 6 votes)
Contextual Ads
More C# Resources
Advertisement
  1. //using System.Text.RegularExpressions;
  2.  
  3. static string CapitalizeString(Match matchString)
  4. {
  5.     string strTemp = matchString.ToString();
  6.     strTemp = char.ToUpper(strTemp[0]) + strTemp.Substring(1, strTemp.Length - 1).ToLower();
  7.     return strTemp;
  8. }
  9.  
  10. // How to use the method
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13.     // This will be transformed into 'The World Is Full Of Complainers...' and so on
  14.     string StringToCap = "The world is full of complainers. And the fact is, nothing comes with a guarantee. Now I don't care if you're the pope of Rome, President of the United States or Man of the Year; something can all go wrong. Now go on ahead, you know, complain, tell your problems to your neighbor, ask for help, and watch him fly. Now, in Russia, they got it mapped out so that everyone pulls for everyone else... that's the theory, anyway. But what I know about is Texas, and down here... you're on your own.";
  15.     string ResultingString = Regex.Replace(StringToCap, @"\w+", new MatchEvaluator(CapitalizeString));
  16.     MessageBox.Show(ResultingString);
  17. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Christiawan on Monday, December 24th 2007 at 02:48 AM

How if I want to do the job capitalize above using Visual C++ 2005?

I\'ve tried your code but there is no RegEx and Match class in Visual C++.
Maybe you know what class similar to RegEx and Match in Visual C++.

Thank you very much.

by Bernd Wechner on Thursday, January 3rd 2008 at 02:20 AM

Nice effort, but clumsy and very wordy implementation. Here\'s some one liners I use to do this kind of stuff:

public static string TitleCase(string Name) { return Regex.Replace(Name.ToLower(), @\"(^|\\W).\", delegate(Match m) { return m.Value.ToUpper(); }); }
public static string CapFirstAll(string Name) { return Regex.Replace(Name, @\"(^|\\W).\", delegate(Match m) { return m.Value.ToUpper(); }); }
public static string CapFirst(string Name) { return Regex.Replace(Name, @\"^.\", delegate(Match m) { return m.Value.ToUpper(); }); }

by David Martin on Thursday, February 21st 2008 at 07:56 AM

Nice oneliners, but those patterns do not always work

by Farid on Thursday, April 17th 2008 at 05:05 AM

With this pattern, it works better :

\"(^|\\\\W+).\"

by Mohsen on Tuesday, November 23rd 2010 at 05:30 AM

Capitalize Word In String

by 0 balance transfer credit cards on Wednesday, December 14th 2011 at 10:21 AM

Why don't you post your own solution Pavlo..... it would have sure taken less time than writing all that.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources