PowerShell 和 StringBuilder
我是 PowerShell 新手,但熟悉 .NET 类。
我在 PowerShell 脚本中使用 System.Text.StringBuilder 。脚本是
Function MyStringFunc([String]$line) {
$r = New-Object -TypeName "System.Collections.Generic.List``1[[System.String]]";
$sb = New-Object -TypeName "System.Text.StringBuilder";
foreach ($c in $line) {
$sb.Append($c);
$r.Add($sb.ToString());
}
return $r;
}
$line1 = "123";
$a = MyStringFunc $line1;
$a
我预期的结果是
1
12
123
但是结果是
Capacity MaxCapacity Length
-------- ----------- ------
16 2147483647 3
123
我做错了什么吗?
I am new in PowerShell but am familiar with .NET classes.
I am using System.Text.StringBuilder
in PowerShell script. The script is that
Function MyStringFunc([String]$line) {
$r = New-Object -TypeName "System.Collections.Generic.List``1[[System.String]]";
$sb = New-Object -TypeName "System.Text.StringBuilder";
foreach ($c in $line) {
$sb.Append($c);
$r.Add($sb.ToString());
}
return $r;
}
$line1 = "123";
$a = MyStringFunc $line1;
$a
I expected the result is
1
12
123
However the result is
Capacity MaxCapacity Length
-------- ----------- ------
16 2147483647 3
123
Did I do something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
StringBuilder 上的几个方法(例如 Append IIRC)会返回 StringBuilder,以便您可以调用更多 StringBuilder 方法。然而,PowerShell 的工作方式是输出所有结果(在 .NET 方法调用的情况下返回值)。在这种情况下,将结果转换为
[void]
以忽略返回值,例如:请注意,您不需要在 PowerShell 中以
;
结束行。但是,如果将多个命令放在同一行,则使用;
' 来分隔这些命令。Several of the methods on StringBuilder like Append IIRC, return the StringBuilder so you can call more StringBuilder methods. However the way PowerShell works is that it outputs all results (return values in the case of .NET method calls). In this case, cast the result to
[void]
to ignore the return value e.g.:Note that you don't need to end lines in
;
in PowerShell. However if you put multiple commands on the same line then use;
' to separate those commands.因为你说你是 PowerShell 新手。我的回答更多的是关于你编写 PowerShell 的方式。
PowerShell 是一种脚本语言,由非开发程序员(例如管理员)使用。您是一名拥有所有知识的 C# 开发人员,但您可以更简单地编写您想编写的内容。在 PowerShell 中,存在使用列表和 Hastables 的语法,请尝试以下操作:
这里有三个函数做同样的事情,我知道字符串构建器在内存方面效率更高一些,但在这种情况下谁在乎呢。
Because you say your are new in PowerShell. My answer is a bit more on the way you are writting PowerShell.
PowerShell is a script language, it's used by non developper programmers such as administrators. You are a C# developper with all your knowledge, but you can write the thing you want to write more simply. In PowerShell it exists syntax to use Lists and Hastables, try the following :
Here are three functions doing the same thing, I know that stringbuilders are a bit more efficient in memory, but in this case who cares.
foreach
不会迭代字符串中的字符,而是将其视为单个项目。因此,我们必须将字符串转换为[char[]]
(或使用$line.GetEnumerator()
)。表达式
$sb.Append($c)
获取构建器实例。在 PowerShell 中,它被写入输出。如果不需要,我们应该抑制此输出($null = ...
或... > $null
或... | Out- Null
)。这段代码产生了预期的输出:
foreach
does not iterate through characters in a string, it treats it as a single item. So that we have to cast a string to[char[]]
(or use$line.GetEnumerator()
).The expression
$sb.Append($c)
gets the builder instance. In PowerShell it gets written to the output. As far as it is unwanted we should suppress this output ($null = ...
or... > $null
or... | Out-Null
).The expected output is produced by this code: