我是 PowerShell 的新手,但不是脚本。
为什么这个脚本:
$usr = "john.doe"
$usrname = $usr -split ".", 0, "simplematch"
$fullname = upperInitial($usrname[0]) + upperInitial($usrname[1])
write-host "Hello $fullname"
function upperInitial($upperInitialString) {
return $upperInitialString.substring(0, 1).toupper() + $upperInitialString.substring(1).tolower()
}
只回复我“Hello John”而不是“Hello John Doe”?
它不是将upperInitial
函数的第二次调用视为函数,而是将其视为第一次调用函数的参数。
这些工作中的任何一个:
$fullname = "$(upperInitial($usrname[0])) $(upperInitial($usrname[1]))"
write-host "Hello $fullname"
以上使用子表达式运算符$()
来执行双引号字符串中的函数。
$fullname = (upperInitial($usrname[0])) + ' ' + (upperInitial($usrname[1]))
write-host "Hello $fullname"
这个函数按照您的预期组合了两个函数的结果,尽管我还添加了一个空格字符,否则它就是 JohnDoe。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句