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

0

2 Answers 2

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(); 
Sign up to request clarification or add additional context in comments.

6 Comments

A little bit of context/explanation might be nice! +1 for the script/language-independent solution, though.
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.
Also, please don't link the TextInfo word to some blog. Link to MSDN directly.
@AndersTornblad: thanks for the valuable point, I have updated the answer with string inString = "LUXOR".ToLower(); and also corrected the link
@Graham'anu' - Beware that in the second one, you are creating 4 extra strings (Substring x 2) and (ToLower x 2)
|
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));
            }

        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.