How to change a text's case first letter and the rest of the letters willingly?

3282 views c#
2

I've faced an issue of casing a text however I want, and I've solved it, as viewable in the answer for this question.

The problem was that I couldn't uppercase the first letter of a word while keeping all the other letters with whatever case they already had, using built in functions.

I ended up creating my own function for the matter which is described about in the answer section.

answered question

2 Answers

6

I've made a function to handle any type of text casing anyone desires.

public class ChangeTextCaseType
{
    public class GetChangeTextCaseType
    {
        public int// 0 - remain as it is, 1 - lower cased, 2 - upper cased, 3 - inverted case
            WordsFirstLetterCase
            , WordsRestLettersCase;

        public string SourceText;
    }

    public static string ChangeTextCase(GetChangeTextCaseType GetChangeTextCase)
    {
        GetChangeTextCaseType FunctionGet = GetChangeTextCase;
        string FunctionResult = "";

        if (FunctionGet.SourceText.Length > 0)
        {
            bool StartOfAWord = true;

            if (FunctionGet.SourceText[0] >= 'a' && FunctionGet.SourceText[0] <= 'z')
            {
                StartOfAWord = false;

                if (FunctionGet.WordsFirstLetterCase == 2 || FunctionGet.WordsFirstLetterCase == 3)
                    FunctionResult += (char)(FunctionGet.SourceText[0] - 32);
                else
                    FunctionResult += FunctionGet.SourceText[0];
            }
            else
            {
                if (FunctionGet.SourceText[0] >= 'A' && FunctionGet.SourceText[0] <= 'Z')
                {
                    StartOfAWord = false;

                    if (FunctionGet.WordsFirstLetterCase == 1 || FunctionGet.WordsFirstLetterCase == 3)
                        FunctionResult += (char)(FunctionGet.SourceText[0] + 32);
                    else
                        FunctionResult += FunctionGet.SourceText[0];
                }
                else
                    FunctionResult += FunctionGet.SourceText[0];
            }

            for (int SourceTextIndex = 1; SourceTextIndex < FunctionGet.SourceText.Length; SourceTextIndex++)
                if (FunctionGet.SourceText[SourceTextIndex] >= 'a' && FunctionGet.SourceText[SourceTextIndex] <= 'z')
                    if (StartOfAWord == true)
                    {
                        StartOfAWord = false;

                        if (FunctionGet.WordsFirstLetterCase == 2 || FunctionGet.WordsFirstLetterCase == 3)
                            FunctionResult += (char)(FunctionGet.SourceText[SourceTextIndex] - 32);
                        else
                            FunctionResult += FunctionGet.SourceText[SourceTextIndex];
                    }
                    else
                    {
                        if (FunctionGet.WordsRestLettersCase == 2 || FunctionGet.WordsRestLettersCase == 3)
                            FunctionResult += (char)(FunctionGet.SourceText[SourceTextIndex] - 32);
                        else
                            FunctionResult += FunctionGet.SourceText[SourceTextIndex];
                    }
                else
                {
                    if (FunctionGet.SourceText[SourceTextIndex] >= 'A' && FunctionGet.SourceText[SourceTextIndex] <= 'Z')
                        if (StartOfAWord == true)
                        {
                            StartOfAWord = false;

                            if (FunctionGet.WordsFirstLetterCase == 1 || FunctionGet.WordsFirstLetterCase == 3)
                                FunctionResult += (char)(FunctionGet.SourceText[SourceTextIndex] + 32);
                            else
                                FunctionResult += FunctionGet.SourceText[SourceTextIndex];
                        }
                        else
                        {
                            if (FunctionGet.WordsRestLettersCase == 1 || FunctionGet.WordsRestLettersCase == 3)
                                FunctionResult += (char)(FunctionGet.SourceText[SourceTextIndex] + 32);
                            else
                                FunctionResult += FunctionGet.SourceText[SourceTextIndex];
                        }
                    else
                    {
                        if (StartOfAWord == false)
                            StartOfAWord = true;

                        FunctionResult += FunctionGet.SourceText[SourceTextIndex];
                    }
                }
        }

        return FunctionResult;
    }
}

usage :

string abc = "ABCd efG5-!rt %2r$b# RB";
abc = TextTools.ChangeTextCaseType.ChangeTextCase(new TextTools.ChangeTextCaseType.GetChangeTextCaseType() { SourceText = abc, WordsFirstLetterCase = 2, WordsRestLettersCase = 1 });

posted this
2

You can use the TextInfo ToTitleCase() method:

static void Main()
{
    TextInfo info = CultureInfo.CurrentCulture.TextInfo;
    string input = "some lowercase text";
    Console.WriteLine(info.ToTitleCase(input)); // Outputs 'Some lowercase text'
    Console.ReadKey(true);
}

posted this

Have an answer?

JD

Please login first before posting an answer.