2

My goal is to know how this kind of text $var1 = "my-name-is" convert to camelCase"myNameIs". And this type $var2 = "my_Name_is" convert to PascalCase"MyNameIs"

Have no idea how to do it

seyshanbe
  • 31
  • 1
  • 4
  • What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Jun 16 '21 at 16:59
  • what I should do if I haven't tried something? because I have no Idea have to do that – seyshanbe Jun 16 '21 at 17:03
  • I would start by finding some good PowerShell reference books and start reading. Posting questions that look like "do my homework/class exercise" is strongly discouraged by this community; we'll help if we see effort to solve it on your part. – Jeff Zeitlin Jun 16 '21 at 17:05
  • I got it. Sorry – seyshanbe Jun 16 '21 at 17:07

4 Answers4

10

You might use the .NET TextInfo.ToTitleCase(String) method for this.

For PascalCase this is quiet straight forward:

$Text = $Var1 -Replace '[^0-9A-Z]', ' '
(Get-Culture).TextInfo.ToTitleCase($Text) -Replace ' '

For camelCase there is a little more at stake:

$First, $Rest = $Var2 -Replace '[^0-9A-Z]', ' ' -Split ' ',2
$First.Tolower() + (Get-Culture).TextInfo.ToTitleCase($Rest) -Replace ' '

Explanation

'my_Name_is' -Replace '[^0-9A-Z]', ' '

Replaces any character that isn't Alphanumeric with a space (result: my Name is).
(Note that PowerShell is case insensitive by default)

'my Name is' -Split ' ',2

Splits the word ('my') from the rest ('Name is').

$First, $Rest = 'my', 'Name is'

Stores the first word in $First and the rest in $Rest.

$First.Tolower() + (Get-Culture).TextInfo.ToTitleCase($Rest)

Puts the first word in lowercase and the rest in title case (result: myName Is)

'myName Is' -Replace ' '

Removes any spaces (result: myNameIs).

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
iRon
  • 20,463
  • 10
  • 53
  • 79
  • 1
    doesn't your "Replaces any character that isn't Alphanumeric" expression not be '[^0-9a-zA-Z]'? I think you're missing the lowercase characters and the comma may be seen as a literal character. Everything else: excellent answer! – GerardV Nov 04 '22 at 08:35
  • 2
    @GerardV, you are correct about the comma (I have updated the answer for this) -thanks. For the lowercase, PowerShell is case insensitive by nature and therefore `A-Z` will capture both upper- and lowercase. For a case sensitive replace, you will need the `-CReplace` operator. – iRon Nov 04 '22 at 09:04
5

PowerShell (Core) 7+ solution, taking advantage of the ability to use script blocks ({ ... }) to perform dynamic replacements via the regex-based -replace operator:

# camelCase (separator char. in input string is "-")
PS> "my-name-is" -replace '-(\p{L})', { $_.Groups[1].Value.ToUpper() }
myNameIs

# PascalCase (separator char. in input string is "_")
PS> "my_Name_is" -replace '(?:^|_)(\p{L})', { $_.Groups[1].Value.ToUpper() }
MyNameIs

Windows PowerShell solution:

Direct use of the underlying .NET APIs is required, because use of script blocks as -replace's replacement operand aren't supported in this edition.

# camelCase
PS> [regex]::Replace("my-name-is", '(?i)-(\p{L})', { $args[0].Groups[1].Value.ToUpper() })
myNameIs

# PascalCase
PS> [regex]::Replace("my_Name_is", '(?i)(?:^|_)(\p{L})', { $args[0].Groups[1].Value.ToUpper() })
MyNameIs
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • could you help me. How I can get unique and alphabetical sorted string, for example "dasdas" -> "ads" @mklement0 – seyshanbe Jun 20 '21 at 18:10
  • @seyshanbe: `-join ("dasdas".ToCharArray() | Sort-Object -Unique)`. In general, please don't ask follow-up questions in comments and instead create a _new_ question post. – mklement0 Jun 20 '21 at 19:43
2

This function will help you out!

$var1 = "my-name-is"

function ToCamelCase($str){
    
    $bits = @()
    foreach($token in $str.Split('-')) {
        $newBit = $token[0].ToString().ToUpper(), $token.TrimStart($token[0]) -join ''
        $bits +=$newBIt
    }
    $bits -join ''

}

PS> ToCamelCase $VAR1
MyNameIs
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
0

To convert dashed string to Camel Case you can use one-liner:

$t.Substring(0,1).ToLower() + ((Get-Culture).TextInfo.ToTitleCase(($t.ToLower() -replace "-", " ")) -replace " ", "").Substring(1)
Oleg Tretiakov
  • 159
  • 1
  • 12