Home > DeveloperSection > Forums > How to capitalize every third letter of a string in C#?
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    March-06-2014 10:57 PM

 C# C# 
Ratings:


 1 Reply(s)
 1211  View(s)
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



Pravesh Singh

Total Post:411

Points:2881
Posted on    March-06-2014 11:15 PM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick