Capitalise first letter of names (fred bloggs = Fred Bloggs)

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1136 Views - Last Post: 04 April 2013 - 11:33 AM Rate Topic: -----

#1 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 190
  • Joined: 26-April 10

Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 08:36 AM

So I'm just wondering about what technique people would use for getting a name (Or whatever string they want) and capitalising all the first letters of each word.
I have no problem with doing this, I'm just wondering if there is a better way to what I'm doing (I assume there will be, I just whipped this up in 5 minutes)

This is what I've been using
      public static string CapitaliseFirstLetters(this string str)
      {
         string temp = "";

         if (str.Length == 0)
            return str;

         temp += str[0].ToString().ToUpper();
         for (int i = 1; i < str.Length; i++)
         {
            if (i > 1)
               if (str[i - 1] == ' ')
               {
                  temp += str[i].ToString().ToUpper();
                  i++;
               }

            temp += str[i];
         }

         return temp;
      }



Hope to see other solutions (:

Is This A Good Question/Topic? 0
  • +

Replies To: Capitalise first letter of names (fred bloggs = Fred Bloggs)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 08:37 AM

Use string's "split" around the spaces.. Iterate through resulting string array by setting the first character of the string to upper. Recombine back into one string.
Was This Post Helpful? 1
  • +
  • -

#3 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 190
  • Joined: 26-April 10

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 08:53 AM

View Postmodi123_1, on 03 April 2013 - 08:37 AM, said:

Use string's "split" around the spaces.. Iterate through resulting string array by setting the first character of the string to upper. Recombine back into one string.


I know you could do it better, but that would be something like this, right?
        public static string CapitaliseFirstLetters(this string str)
        {
            string[] temp = str.Split(' ');
            string tempTwo = "";

            foreach (string s in temp)
            {
                tempTwo += s[0].ToString().ToUpper() + s.Substring(1) + " ";
            }

            return tempTwo.Remove(tempTwo.Length - 1);
        }


This post has been edited by oyyou: 03 April 2013 - 08:56 AM

Was This Post Helpful? 1
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 08:57 AM

That does seem to satisfy the conditions of:
- using split
- string array iteration
- capitalizing the first indexed character in each string
- adding back to a master string.
Was This Post Helpful? 0
  • +
  • -

#5 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 190
  • Joined: 26-April 10

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 08:59 AM

Well I know that :3
But I also a better programmer than me xD I still sometimes make very rookie mistakes.
Was This Post Helpful? 0
  • +
  • -

#6 CodMnk2b  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 07-February 13

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 11:27 AM

Maybe is not necessary for a simple example, but check the point of using a String builder instead of concatenating strings inside a loop:

http://www.red-gate....agement-gotchas
Was This Post Helpful? 2
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 11:38 AM

There is a ToTitleCase() method hidden inside the TextInfo class.

using System;
using System.Collections.Generic;
using System.Globalization;     // CultureInfo
using System.Linq;
using System.Text;
using System.Threading;         // Thread
using System.Threading.Tasks;

namespace ConsTest {
    class Program {
        static void Main(string[] args) {
            CultureInfo cInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo tInfo = cInfo.TextInfo;
            Console.WriteLine(tInfo.ToTitleCase("hello there"));

            Console.ReadKey();
        }
    }
}

(although, it's easy enough to implement; I think it could be done with Linq as well.)
Was This Post Helpful? 3
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 11:54 AM

LINQ:
            string sTest = "hello there, you. how are YOU today?";

            string sTitled = String.Join(" ",(from word in sTest.Split(' ')
                              select word[0].ToString().ToUpper() + word.Substring(1).ToLower()));

            Console.WriteLine(sTitled);

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 12:08 PM

This is slightly more complicated if there might be more than one space between words:

            string sTest = "hello there,     you. how   are YOU today?";

            string sTitled = String.Join(" ",(from word in sTest.Split(new char[]{' '},
                                                  StringSplitOptions.RemoveEmptyEntries)
                              select word[0].ToString().ToUpper() + word.Substring(1).ToLower()));

            Console.WriteLine(sTitled);

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 01:16 PM

Or in VB.net
Dim InputText = "dream in code"
Dim TitleCaseText = My.Application.Culture.TextInfo.ToTitleCase( InputText )



andrewsw: Great find there .net has some useful stuff hidden away, out in the boondocks namespaces.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 01:29 PM

Completely off topic, but I have adopted the habit of adding the classes as comments to using (import) statements:

using System.Globalization;     // CultureInfo

I do this the first time Visual Studio (kindly!) creates the using statement for me, and try to add further class-names as I go along. This helps me keep track of why I have included certain namespaces and to decide if, occasionally, I can remove them. I recommend this tactic :)/>

@Adam Thank you. It would be useful, I suppose, to create a String extension method of TitleCase(), or one just for names and titles.

cf Pepé le Pew and Jean-Claude V(v?)an Damme ??

This post has been edited by andrewsw: 03 April 2013 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 01:38 PM

namespace ExtensionMethods {
    public static class MyExtensions {
        public static string TitleCase(this String str) {
            return String.Join(" ",(from word in str.Split(new char[]{' '},
                                                  StringSplitOptions.RemoveEmptyEntries)
                              select word[0].ToString().ToUpper() + word.Substring(1).ToLower()));
        }
    }
}

using ExtensionMethods;

namespace ConsTest {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("howdy doody!".TitleCase());
            
            Console.ReadKey();
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 01:46 PM

Some here, but with me it NuGet packages I've made, on updated version of Exts (I plan to upload it sometime).
Since they contain loads of useful extensions and classes, that I use frequently.

Just import the namespace and I have operators for doing bound checks.
If LowerBound._() < Value <= Upper Then


vb.net (I don't know if C#) you get your namespace to start at the Global.{} level.
In a separate Library Project DLL
Namespace Global.Exts.Comparisions
  <HideModuleName>
  Public Module Exts
''
'' etc
  End Module
End Namespace


Then in use
Imports Exts.Comparisions



That use of Global on the namespace allows separate DLLs to coexist and share the same namespace. Yeah in theory and practice you screw yourself if you shi* on something like namespace called System

This post has been edited by AdamSpeight2008: 03 April 2013 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 01:59 PM

@Adam There is a feature I have used in JS called a polymorphic function (or method), although I don't know if it would work in quite the same way in C#(?). That is, create someMethod() that first checks if this method already exists, in which case assign this (existing) method as the version to use; otherwise, create and assign a new, (extension) version. The idea is that ONLY the first time the method is used, is the correct version assigned.

Well, actually, it must be available in C#, being a fully OO language. But I assume the syntax is slightly different.

[Sorry, I'm side-tracking.]

This post has been edited by andrewsw: 03 April 2013 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3819
  • View blog
  • Posts: 13,537
  • Joined: 12-December 12

Re: Capitalise first letter of names (fred bloggs = Fred Bloggs)

Posted 03 April 2013 - 02:07 PM

View PostAdamSpeight2008, on 03 April 2013 - 08:46 PM, said:

Some here, but with me it NuGet packages I've made, on updated version of Exts (I plan to upload it sometime).
Since they contain loads of useful extensions and classes, that I use frequently.

Just import the namespace and I have operators for doing bound checks.
If LowerBound._() < Value <= Upper Then


vb.net (I don't know if C#) you get your namespace to start at the Global.{} level.
In a separate Library Project DLL
Namespace Global.Exts.Comparisions
  <HideModuleName>
  Public Module Exts
''
'' etc
  End Module
End Namespace


Then in use
Imports Exts.Comparisions



That use of Global on the namespace allows separate DLLs to coexist and share the same namespace. Yeah in theory and practice you screw yourself if you shi* on something like namespace called System

In C# the equivalent, I believe, is global::SomeClass.

Using namespace-aliases is very helpful, but we don't see it that often (strangely):

using colAlias = System.Collections;

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2