PowerShell programming puzzle: Convert snake_case to PascalCase in one line

Raymond Chen

A friend posed this PowerShell programming puzzle (P³), which represents an actual problem he had to solve:

Given a string in $t in snake_case or SHOUTY_SNAKE_CASE, return the corresponding PascalCase string. You may assume no consecutive underscores.

The initial version went like this:

$uc = $true; 
[string]::Concat(($t.ToCharArray() | % { 
  if ($uc) { ($_ -as [string]).ToUpper(); $uc = $false; } 
  elseif ($_ -eq '_') { $uc = $true; } 
  else { ($_ -as [string]).ToLower();} 

This is a straightforward translation of the requirements: Walk through the string one character at a time. Capitalize the first letter and any letter that follows an underscore. Everything else becomes lowercase.

I countered with this crazy one-liner:

(Get-Culture).TextInfo.ToTitleCase(($t.ToLower() -replace "_", " ")) -replace " ", ""

This version cheats like crazy, but hey, we’re code-golfing. It relies on the capitalization rules of the English language, and it assumes that every word starts with a letter.

The idea is to take advantage of To­Title­Case, which capitalizes the first letter of each space-separated word. All we have to do is transform the string into something that we can feed into To­Title­Case.

So start with the string, convert it entirely to lowercase (to avoid the feature of To­Title­Case that preserves all-capitalized words), and change the underscores to spaces. Now we can ask To­Title­Case to capitalize the first letter of each word. Then we compress out the spaces with a final -replace.

A less crazy version would be something like this:

[regex]::replace($t.ToLower(), '(^|_)(.)', { $args[0].Groups[2].Value.ToUpper()})

First, we convert the entire string to lowercase. Then we search for each character that comes after an underscore (considering the first character of the string to be after an imaginary leading underscore) and capitalize it. The underscore itself is not returned, which causes it to vanish.