Advertise with us

How to capitalize every third letter of a string in C#?

Posted by  Jayden Bell
C# 
C#
 2326  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. 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