How to Convert String to Camel Case in PowerShell?

Camel case is a popular programming naming convention in which the first letter of the first word is lowercase, and the first letter of each subsequent word is capitalized, with no spaces or punctuation between words. For example, “convert string to camel case” becomes “convertStringToCamelCase.” This naming convention is often used in variable and function names. In this PowerShell tutorial, I will show you different methods to convert a string to camel case in PowerShell.

To convert a string to camel case in PowerShell, you can use a function that splits the string into words, capitalizes the first letter of each word except the first one, and then joins them back together. For example, the function ConvertTo-CamelCase takes an input string, converts it to lowercase, splits it into words, capitalizes the necessary letters, and joins them to form a camel case string like “convertStringToCamelCase”.

Method 1: Using Regular Expressions

Regular expressions (regex) are powerful for string manipulation. In PowerShell, you can use the -replace operator with regex to achieve camel case conversion.

Example

Here is an example of converting string to Camel case in PowerShell using regular expressions.

function ConvertTo-CamelCase {
    param (
        [string]$inputString
    )

    # Convert the string to lowercase and split it into words
    $words = $inputString.ToLower() -split '\s+'

    # Capitalize the first letter of each word except the first one
    for ($i = 1; $i -lt $words.Length; $i++) {
        $words[$i] = -join ([string]$words[$i][0].ToUpper(), $words[$i].Substring(1))
    }

    # Join the words together without spaces
    $camelCaseString = -join $words
    return $camelCaseString
}

# Example usage
$input = "convert string to camel case"
$camelCase = ConvertTo-CamelCase -inputString $input
Write-Output $camelCase  # Output: convertStringToCamelCase

Explanation

  1. Parameter Definition: The function ConvertTo-CamelCase takes a single string parameter $inputString.
  2. Lowercase Conversion and Splitting: The input string is converted to lowercase and split into an array of words using the -split operator with a regex pattern that matches any whitespace (\s+).
  3. Capitalization: A for loop iterates through the array starting from the second word (index 1). For each word, the first character is converted to uppercase and concatenated with the rest of the word.
  4. Joining Words: The -join operator concatenates the array of words back into a single string.
  5. Output: The resulting camel case string is returned.

Check out How to Split String by Tab Character in PowerShell?

Method 2: Using String Manipulation

Let me show you now how to use string manipulation to convert a string to Camel case in PowerShell without relying heavily on regex.

Below is an example of how to do it.

Example

function ConvertTo-CamelCaseSimple {
    param (
        [string]$inputString
    )

    # Split the string into words
    $words = $inputString.Split(" ")

    # Capitalize the first letter of each word except the first one
    for ($i = 1; $i -lt $words.Length; $i++) {
        $words[$i] = $words[$i].Substring(0, 1).ToUpper() + $words[$i].Substring(1).ToLower()
    }

    # Join the words together
    $camelCaseString = $words[0].ToLower() + ($words[1..$words.Length] -join "")
    return $camelCaseString
}

# Example usage
$input = "convert string to camel case"
$camelCase = ConvertTo-CamelCaseSimple -inputString $input
Write-Output $camelCase  # Output: convertStringToCamelCase

Explanation

  1. Parameter Definition: The function ConvertTo-CamelCaseSimple takes a single string parameter $inputString.
  2. Splitting: The input string is split into an array of words using the Split method with a space as the delimiter.
  3. Capitalization: A for loop iterates through the array starting from the second word (index 1). The first character is converted to uppercase for each word, and the rest is converted to lowercase.
  4. Joining Words: The first word is converted to lowercase, and the rest of the words are concatenated using the -join operator.
  5. Output: The resulting camel case string is returned.

I executed the above PowerShell code, and you can see the output in the screenshot below:

Convert String to Camel Case in PowerShell

Check out PowerShell String Replace

Method 3: Using the TextInfo Class

You can also use the TextInfo class, which is a .NET class in PowerShell, for more advanced string manipulations.

Example

Here is an example to help you understand it better.

function ConvertTo-CamelCaseWithTextInfo {
    param (
        [string]$inputString
    )

    # Create a TextInfo object
    $textInfo = [System.Globalization.CultureInfo]::CurrentCulture.TextInfo

    # Convert the string to title case
    $titleCaseString = $textInfo.ToTitleCase($inputString.ToLower())

    # Remove spaces and convert the first character to lowercase
    $camelCaseString = $titleCaseString -replace ' ', ''
    $camelCaseString = $camelCaseString.Substring(0, 1).ToLower() + $camelCaseString.Substring(1)

    return $camelCaseString
}

# Example usage
$input = "convert string to camel case"
$camelCase = ConvertTo-CamelCaseWithTextInfo -inputString $input
Write-Output $camelCase  # Output: convertStringToCamelCase

Explanation

  1. Parameter Definition: The function ConvertTo-CamelCaseWithTextInfo takes a single string parameter $inputString.
  2. TextInfo Object: A TextInfo object is created using the current culture’s text information.
  3. Title Case Conversion: The input string is converted to title case using the ToTitleCase method, which capitalizes the first letter of each word.
  4. Removing Spaces: The spaces are removed using the -replace operator.
  5. Lowercase First Character: The first character of the resulting string is converted to lowercase.
  6. Output: The resulting camel case string is returned.

I executed the above PowerShell script, and you can see the output in the screenshot below:

How to Convert String to Camel Case in PowerShell

Conclusion

In this tutorial, I explained how to convert a string to camel case in PowerShell using several methods, such as regular expressions, simple string manipulation, or leveraging .NET classes.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.