Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Upper case words

    In PHP there is a function called ucwords where it capitalizes the first letter of each word in a string, and the rest of the word is set to lowercase.

    Is there a similar string function in C#?

    I want to change this: "hi there"
    to this: "Hi There"

  • #2
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    as far as i know... there is no predefined way to capitalize strings in C#...
    you have to code for it...
    - NS 666
    .net DEVILoper

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    In that case does anyone know of a place I could grab a function that does this? It must have been done before....

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    This could be a fun one to use a Regex on. Hopefully I'll get time to give it a shot later. But if your string is always separated with spaces, you can just split on the space, and put the peices back together:
    Code:
    		private static string Capital(string strVal)
    		{
    			if(strVal == string.Empty)
    			{
    				throw new ArgumentNullException("Boo", "string not passed");
    			}
    
    			string[] parts = strVal.Split(" ".ToCharArray());
    			strVal = null;
    			for(int i = 0; i < parts.Length; i++)
    			{
    				strVal += parts[i].Substring(0, 1).ToUpper() + parts[i].Substring(1).ToLower() + " ";
    			}
    
    			return strVal;
    		}
    
    // example
    Capital("hello world");
    Good luck;

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, this is great


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •