Powershell:字符串被多次添加到另一个字符串
Powershell: String is added more than once to another string
在构建文本 blob 的脚本中,有一点可以将 "summary" 文本块添加到所述 blob。
虽然脚本只生成一次摘要文本,但它会多次添加到文本 blob 中。
这是 PowerShell 脚本:
#
# Test_TextAppend.ps1
#
$reportMessage = "Body of Report Able Was I Ere I Saw Elba"; # build "report" text
$fruitList = [System.Collections.ArrayList]@();
$vegetableList = [System.Collections.ArrayList]@();
[void]$fruitList.Add("apple");
# Generate a "summary" that includes the contents of both lists
function GenerateSummary()
{
[System.Text.StringBuilder]$sumText = New-Object ("System.Text.StringBuilder")
$nameArray = $null;
[string]$nameList = $null;
if ($fruitList.Count -gt 0)
{
$nameArray = $fruitList.ToArray([System.String]);
$nameList = [string]::Join(", ", $nameArray);
$sumText.AppendFormat("The following fruits were found: {0}`n",
$nameList);
}
if ($vegetableList.Count -gt 0)
{
$nameArray = $vegetableList.ToArray([System.String]);
$nameList = [string]::Join(", ", $nameArray);
$sumText.AppendFormat("The following vegetables were found: {0}`n",
$nameList);
}
if ($sumText.Length -gt 0)
{
$sumText.Append("`n");
}
return ($sumText.ToString());
}
[string]$summary = (GenerateSummary);
if (![string]::IsNullOrEmpty($summary)) # if there is any "summary" text, prepend it
{
$reportMessage = $summary + $reportMessage;
}
Write-Output $reportMessage
这是运行时的结果:
The following fruits were found: apple
The following fruits were found: apple
The following fruits were found: apple
Body of Report Able Was I Ere I Saw Elba
我使用了代码块而不是 blockquote,因为固定宽度的字体显示了额外的前导空格。
问题:为什么摘要文本重复了三次而不是只重复了一次?
阅读about_Return:
LONG DESCRIPTION
The Return keyword exits a function, script, or script block. It
can be used to exit a scope at a specific point, to return a
value, or to indicate that the end of the scope has been reached.
Users who are familiar with languages like C or C# might want to
use the Return keyword to make the logic of leaving a scope
explicit.
In Windows PowerShell, the results of each statement are returned
as output, even without a statement that contains the Return
keyword. Languages like C or C# return only the value or values
that are specified by the Return keyword.
接下来所有三个语句的结果构成函数的输出:
$sumText.AppendFormat("The following fruits were found: {0}`n", $nameList);
$sumText.Append("`n");
return ($sumText.ToString());
(如果 $vegetableList.Count -gt 0
也来自下一条语句):
$sumText.AppendFormat("The following vegetables were found: {0}`n", $nameList);
在构建文本 blob 的脚本中,有一点可以将 "summary" 文本块添加到所述 blob。
虽然脚本只生成一次摘要文本,但它会多次添加到文本 blob 中。
这是 PowerShell 脚本:
#
# Test_TextAppend.ps1
#
$reportMessage = "Body of Report Able Was I Ere I Saw Elba"; # build "report" text
$fruitList = [System.Collections.ArrayList]@();
$vegetableList = [System.Collections.ArrayList]@();
[void]$fruitList.Add("apple");
# Generate a "summary" that includes the contents of both lists
function GenerateSummary()
{
[System.Text.StringBuilder]$sumText = New-Object ("System.Text.StringBuilder")
$nameArray = $null;
[string]$nameList = $null;
if ($fruitList.Count -gt 0)
{
$nameArray = $fruitList.ToArray([System.String]);
$nameList = [string]::Join(", ", $nameArray);
$sumText.AppendFormat("The following fruits were found: {0}`n",
$nameList);
}
if ($vegetableList.Count -gt 0)
{
$nameArray = $vegetableList.ToArray([System.String]);
$nameList = [string]::Join(", ", $nameArray);
$sumText.AppendFormat("The following vegetables were found: {0}`n",
$nameList);
}
if ($sumText.Length -gt 0)
{
$sumText.Append("`n");
}
return ($sumText.ToString());
}
[string]$summary = (GenerateSummary);
if (![string]::IsNullOrEmpty($summary)) # if there is any "summary" text, prepend it
{
$reportMessage = $summary + $reportMessage;
}
Write-Output $reportMessage
这是运行时的结果:
The following fruits were found: apple
The following fruits were found: apple
The following fruits were found: apple
Body of Report Able Was I Ere I Saw Elba
我使用了代码块而不是 blockquote,因为固定宽度的字体显示了额外的前导空格。
问题:为什么摘要文本重复了三次而不是只重复了一次?
阅读about_Return:
LONG DESCRIPTION
The Return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.
Users who are familiar with languages like C or C# might want to use the Return keyword to make the logic of leaving a scope explicit.
In Windows PowerShell, the results of each statement are returned as output, even without a statement that contains the Return keyword. Languages like C or C# return only the value or values that are specified by the Return keyword.
接下来所有三个语句的结果构成函数的输出:
$sumText.AppendFormat("The following fruits were found: {0}`n", $nameList);
$sumText.Append("`n");
return ($sumText.ToString());
(如果 $vegetableList.Count -gt 0
也来自下一条语句):
$sumText.AppendFormat("The following vegetables were found: {0}`n", $nameList);