您可能会使用.NETTextInfo.ToTitleCase(String) method https://learn.microsoft.com/dotnet/api/system.globalization.textinfo.totitlecase为了这。
For 帕斯卡案例这非常简单:
$Text = $Var1 -Replace '[^0-9A-Z]', ' '
(Get-Culture).TextInfo.ToTitleCase($Text) -Replace ' '
For 骆驼香烟盒还有更多的利害关系:
$First, $Rest = $Var2 -Replace '[^0-9A-Z]', ' ' -Split ' ',2
$First.Tolower() + (Get-Culture).TextInfo.ToTitleCase($Rest) -Replace ' '
解释
'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
拆分单词 ('my'
)从其余的('Name is'
).
$First, $Rest = 'my', 'Name is'
将第一个单词存储在$First
其余的在$Rest
.
$First.Tolower() + (Get-Culture).TextInfo.ToTitleCase($Rest)
将第一个单词小写,其余单词大写(结果:myName Is
)
'myName Is' -Replace ' '
删除所有空格(结果:myNameIs
).