tags:

views:

807

answers:

5

I have a string of text (about 5-6 words mostly) that I need to convert.

Currently the text looks like:

THIS IS MY TEXT RIGHT NOW

I want to convert it to:

This Is My Text Right Now

I can loop through my collection of strings, but not sure how to go about performing this text modification.

+17  A: 

string s = "THIS IS MY TEXT RIGHT NOW";

s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.toLower());

jspcal
wow, I wouldn't ahve guessed that one!
mrblah
Awesome! Never knew about that.
GrayWizardx
Haha, really? That's where it is? I love .NET but some of the API designers are real jerks.
George Mauer
Note that while this method will do what the questioner has asked, it's a naive algorithm that just capitalizes each word, without regard to what kind of word it is. It's not really "title case" since rules for title casing differ in different languages. It's not even correct for English. For instance, the title "about a boy" should be "About a Boy" in English, but this method would give "About A Boy". If you want *true* title case, you'll have to write your own method.
Kyralessa
I wouldn't call them jerks. The thing with "ToTitleCase" is that it's culture-dependent, hence it has to be in the System.Globalization namespace. Going through CurrentThread is just done to get the current Culture of the Thread (Be aware that this may cause different behavior if the user has a different Locale!). You could as well do "CultureInfo.InvariantCulture.TextInfo.ToTitleCase()", which may be better as InvariantCulture behaves the same on all cultures. Out of interest George: Where would you put a Culture-Specific String function?
Michael Stum
Also note the comments about ALL UPPERCASE strings: http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx
Michael Stum
+2  A: 

There's a couple of ways to go about converting the first char of a string to upper case.

The first way is to create a method that simply caps the first char and appends the rest of the string using a substring:

public string UppercaseFirst(string s)
    {
        return char.ToUpper(s[0]) + s.Substring(1);
    }

The second way (which is slightly faster) is to split the string into a char array and then re-build the string:

public string UppercaseFirst(string s)
    {
        char[] a = s.ToCharArray();
        a[0] = char.ToUpper(a[0]);
        return new string(a);
    }
Jamie Dixon
A: 

I don't know if the solution below is more or less efficient than jspcal's answer, but I'm pretty sure it requires less object creation than Jamie's and George's.

string s = "THIS IS MY TEXT RIGHT NOW";
StringBuilder sb = new StringBuilder(s.Length);
bool capitalize = true;
foreach (char c in s) {
    sb.Append(capitalize ? Char.ToUpper(c) : Char.ToLower(c));
    capitalize = !Char.IsLetter(c);
}
return sb.ToString();
ephemient
If we're concerned with object creation, why not do it in-place with a for loop indexing over s instead of using a StringBuilder?
jball
Strings are immutable.
ephemient
+1  A: 

Untested but something like this should work:

var phrase = "THIS IS MY TEXT RIGHT NOW";
var rx = new System.Text.RegularExpressions.Regex(@"(?<=\w)\w");
var newString = rx.Replace(phrase,new MatchEvaluator(m=>m.Value.ToLowerInvariant()));

Essentially it says "preform a regex match on all occurrences of an alphanumeric character that follows another alphanumeric character and then replace it with a lowercase version of itself"

George Mauer
+2  A: 

I probably prefer to invoke the ToTitleCase from CultureInfo (System.Globalization) then Thread.CurrentThread (System.Threading)

string s = "THIS IS MY TEXT RIGHT NOW";
s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());

but it should be the same as jspcal solution

References:

Filippo