Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert strings like "ARGO" to "Argo" and "LIONEL BARRYMORE" to "Lionel Barrymore"

I've got this code:

C#
private string rightCase(string goofyStr)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;
    return textInfo.ToTitleCase(goofyStr);
}


...which I got form here (http://support.microsoft.com/kb/312890/en-us) and which I call like so:

C#
return string.Format("{0},{1},{2},", rightCase(category), rightCase(film), rightCase(actor));


...but it doesn't work ("ARGO" is returned unchaned, as "ARGO" etc.)

What am I doing wrong?
Posted

You can try something like -
C#
CultureInfo.CurrentCulture.TextInfo.ToTitleCase("ARGO")
 
Share this answer
 
C#
// Creates a TextInfo based on the "en-US" culture.
      

  private string rightCase(string goofyStr)
    {
       TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
       return myTI.ToTitleCase(goofyStr);
    }
 
Share this answer
 
The ToTitleCase method provides an arbitrary casing behavior which is not necessarily linguistically correct. A linguistically correct solution would require additional rules, and the current algorithm is somewhat simpler and faster. We reserve the right to make this API slower in the future.
So the result is arbitrary and cannot be relied upon.

You could try making the string all lower case with TextInfo.ToLower[^] before using TextInfo.ToTitleCase[^] and see what the result is.
 
Share this answer
 
Comments
B. Clay Shannon 1-Aug-13 10:06am    
Yep, that worked. This:

string s = "JOHN WAYNE";
s = s.ToLower();
s = rightCase(s);
MessageBox.Show(s);

...showed "John Wayne"
C#
private string TitleCase(string goofyStr)
{
    TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;
    return  textInfo.ToTitleCase( goofyStr);
}
 
Share this answer
 
Comments
B. Clay Shannon 1-Aug-13 4:58am    
Nope, this:

private string rightCase(string goofyStr)
{
TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;
return textInfo.ToTitleCase(goofyStr);
}

...didn't work; they remain uppercase.

Nor did this:

string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase("ARGO");
MessageBox.Show(s);

(Message box showed "ARGO")
CodeBlack 1-Aug-13 5:03am    
try this :

private string rightCase(string goofyStr)
{
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
return textInfo.ToTitleCase(goofyStr.ToLower());
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900