[Solved] How to prepend a word with a letter if the first letter of that word is upper-case?


Using a regex:

string paragraphWord = "This is A String of WoRDs";
var reg = new Regex(@"(?<=^|\W)([A-Z])");
Console.WriteLine(reg.Replace(paragraphWord,"k$1"));

Explaining (?<=^|\W)([A-Z])

?<=     positive look behind
^|\W    start of the string or a whitespace character
[A-Z]   A single upper case letter

So we are looking for a single upper case letter proceeded by either the start of the string or a space. We group that character so we can use it in our replacement:

k$1

subs our match with k and the match group $1

0

solved How to prepend a word with a letter if the first letter of that word is upper-case?