HOW TO CAPITALIZE EVERY THIRD LETTER OF A STRING IN C#?

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
C# 
C#
 1648  View(s)
Ratings:
Rate this:

Does anybody know how to capitalize every third letter of a string in C# ? I loop through the whole string with a for loop, but I can't think of the sequence right now.

Thanks in advance

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to capitalize every third letter of a string in C#?

    Hi Jayden,


    I suspect you just want something like this:

    // String is immutable; copy to a char[] so we can modify that in-place

    char[] chars = input.ToCharArray();

    for (int i = 0; i < chars.Length; i += 3)

    {

        chars[i] = char.ToUpper(chars[i]);

    }

    // Now construct a new String from the modified character array

    string output = new string(chars);

    That assumes you want to start capitalizing from the first letter, so "abcdefghij" would become "AbcDefGhiJ". If you want to start capitalizing elsewhere, just change the initial value of i.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!