1

我正在尝试清理richtextbox 中的输入以删除空白行。

当我运行程序时,它会清理输入并运行命令。

这是我在运行命令之前用来清理输入的代码:

$richtextbox1.Font = "Microsoft Sans Serif, 8.25pt"
$richtextbox1.Text = $richtextbox1.Text -replace ";", "`n" -replace ",", "`n" -replace " ", ""

如果它是逗号或分号分隔的列表,它将将每个项目分隔成一个新行,以便以后可以将每一行输入到一个数组中。

我要做的是在richtextbox中找到一行空白并删除该行。从 excel 复制单元格时可能会出现空行。

任何帮助,将不胜感激。如果您有任何问题,我会尽力解释更多。

富文本框中的示例输入:

Computer1, Computer2, Computer3, Computer4, Computer5
Computer6
(Blank space in input)
Computer7

所需的净化输入:

Computer1
Computer2
Computer3
Computer4
Computer5
Computer6
Computer7

**编辑:**我找到了一个草率的解决方案,它适用于我的应用程序,但可能不适用于需要为大量数据执行此操作的人。(我不是一个大程序员,只用它来制作工具,所以我确信有一种方法可以更有效地做到这一点。)

$richtextbox1.Font = "Microsoft Sans Serif, 8.25pt"
$richtextbox1.Text = $richtextbox1.Text -replace ";", "`n" -replace ",", "`n" -replace " ", ""

$computers = $richtextbox1.Text.Split("`n") | % { $_.trim() }
$richtextbox1.Text = ""
foreach ($computer in $computers)
{
    if ($computer -ne "")
    {
        $richtextbox1.AppendText("$computer`r")
    }
}
4

2 回答 2

2

修改这一行:

$richtextbox1.Text = $richtextbox1.Text -replace ";", "`n" -replace ",", "`n" -replace " ", ""

$richtextbox1.Text = $richtextbox1.Text -replace ";", "`n" -replace ",", "`n" -replace " ", "" -replace '(?s)(\n){2,}', "`n" -replace '\n$',''

倒数第二个替换将用单个 \n 替换两个或连续的 \n 字符,以消除内部空白行。最后一个替换是消除最后一个\n。

于 2014-12-20T00:38:32.083 回答
0

我找到了一个草率的解决方案,它适用于我的应用程序,但可能不适用于需要为大量数据执行此操作的人。(我不是一个大程序员,只用它来制作工具,所以我确信有一种方法可以更有效地做到这一点。)

$richtextbox1.Font = "Microsoft Sans Serif, 8.25pt"
$richtextbox1.Text = $richtextbox1.Text -replace ";", "`n" -replace ",", "`n" -replace " ", ""

$computers = $richtextbox1.Text.Split("`n") | % { $_.trim() }
$richtextbox1.Text = ""
foreach ($computer in $computers)
{
    if ($computer -ne "")
    {
    $richtextbox1.AppendText("$computer`r")
    }
}
于 2014-12-19T18:19:31.183 回答