Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用拆分和联接操作Powershell 3字符串_String_Powershell - Fatal编程技术网

String 使用拆分和联接操作Powershell 3字符串

String 使用拆分和联接操作Powershell 3字符串,string,powershell,String,Powershell,我需要使用powershell 3.0拆分连接,将每个单词的第一个字母大写 我一直疯狂地想弄明白这一点 任何帮助都将不胜感激 Function Proper( [switch]$AllCaps, [switch]$title, [string]$textentered=" ") { if ($AllCaps) {$textentered.Toupper()} Elseif ($title) {$textentered -split " "

我需要使用powershell 3.0拆分连接,将每个单词的第一个字母大写

我一直疯狂地想弄明白这一点

任何帮助都将不胜感激

Function Proper( [switch]$AllCaps, [switch]$title, [string]$textentered=" ")
{ 
    if ($AllCaps)
        {$textentered.Toupper()}
    Elseif ($title)
        {$textentered -split " "

        $properwords = $textentered | foreach { $_ }


            $properwords -join " "
            $properwords.substring(0,1).toupper()+$properwords.substring(1).tolower()

            }
}
proper -title "test test"

System.Globalization.TextInfo
类具有可以使用的
ToTitleCase
方法,只需将您的单词正常地连接到字符串中(例如称为
$lowerstring
),然后使用“Get Culture cmdlet:”对该字符串调用该方法:

$titlecasestring = (Get-Culture).TextInfo.ToTitleCase($lowerstring)
对于字符串连接,我倾向于使用以下格式:

$lowerstring = ("the " + "quick " + "brown " + "fox")
但以下内容同样有效:

$lowerstring = 'the','quick','brown','fox' -join " "

$lowerstring = $a,$b,$c,$d -join " "
编辑:

根据您提供的代码,如果您传递的只是字符串中的一个短语,则不需要拆分/连接字符串,因此以下是您需要的

Function Proper{
    Param ([Parameter(Mandatory=$true, Position=0)]
           [string]$textentered,
           [switch]$AllCaps,
           [switch]$Title)

    if ($AllCaps){$properwords = $textentered.Toupper()} 

    if ($title) {
                 $properwords = (Get-Culture).TextInfo.ToTitleCase($textentered)
                }

    if ((!($Title)) -and (!($AllCaps))){
        Return $textentered}
} 

Proper "test test" -AllCaps
Proper "test test" -Title
Proper "test test"
Param()
块中,我将$textentered参数设置为强制参数,并且它必须是第一个参数(Position=0)


如果AllCaps或Title参数均未传递,则原始输入字符串将原封不动地传递回去。

谢谢您的建议。这就是我想到的。它只大写第一个单词的第一个字母。函数本身([switch]$AllCaps,[switch]$title,[string]$textentered=”“){if($AllCaps){$textentered.Toupper()}Elseif($title){$textentered-split”“$properwords=$textentered | foreach{$}$properwords-join”“$properwords.substring(0,1).touper()+$properwords.substring(1).tolower()}Proper-title“测试”