6

I have a string like this LUXOR and I want to convert other letters to lowercase except first letter or the string.that meant, I want this string Luxor from above string. I can convert full string to upper or lower by using ToUpper or ToLower.but how can I do this.hope your help with this.thank you

caldera.sac
  • 4,918
  • 7
  • 37
  • 69

2 Answers2

10

You can make use of TextInfo class that Defines text properties and behaviors, such as casing, that is specific to a writing system.

 string inString = "LUXOR".ToLower();
 TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
 string output = cultInfo.ToTitleCase(inString);

This snippet will give you Luxor in the variable output. this can also be used to capitalize Each Words First Letter

Another option is using .SubString, for this particular scenario of having a single word input:

string inString = "LUXOR"
string outString = inString.Substring(0, 1).ToUpper() + inString.Substring(1).ToLower(); 
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • A little bit of context/explanation might be nice! +1 for the script/language-independent solution, though. – Archimaredes Apr 21 '16 at 12:17
  • Your original answer will actually NOT work for **ALL UPPERCASE** texts, because these are exempt from the conversion ; they are seen as abbreviations! `"this is FBI calling"` will render `"This Is FBI Calling"`. Your updated answer is a bit too complex for the simple task requested. – Anders Marzi Tornblad Apr 21 '16 at 12:19
  • Also, please don't link the `TextInfo` word to some blog. Link to MSDN directly. – Anders Marzi Tornblad Apr 21 '16 at 12:20
  • @AndersTornblad: thanks for the valuable point, I have updated the answer with `string inString = "LUXOR".ToLower();` and also corrected the link – sujith karivelil Apr 21 '16 at 12:22
  • nice one.I like for second method. thank your very much – caldera.sac Apr 21 '16 at 12:52
  • 1
    @Graham'anu' - Beware that in the second one, you are creating 4 extra strings (Substring x 2) and (ToLower x 2) – Chris Dunaway Apr 21 '16 at 18:32
1

Try This,

        string inString = "LUXOR";
        string output = inString.Substring(0, 1) + inString.Substring(1).ToLower();

        string inString2 = "HI HOW ARE YOU";
        string[] finalstring = inString2.Split(' ');

        string output2 = string.Empty;
        foreach (var item in finalstring)
        {
            if (output2 == "")
            {
                output2 = (item.ToUpper().Substring(0, 1) + item.ToLower().Substring(1));
            }
            else
            {
                output2 += " " + (item.ToUpper().Substring(0, 1) + item.ToLower().Substring(1));
            }

        }
Prabhat Sinha
  • 1,500
  • 20
  • 32