在 RichTextBox 的字符串中用分号替换新行 - vb.net

Replacing new lines with semi colons in String from RichTextBox - vb.net

我有一个 RichTextBox,用户可以在其中将产品条形码输入分隔到新行中。

然后我想获取文本并将其传递给字符串,但用分号替换新行以分隔它们。

 Dim products As String = txt_distributorProducts.Text

我可以使用.Replace()方法将“\n”替换为“;”吗?还是这行不通。

感谢您的帮助。

RichTextBox 有一个 Lines 属性 即 returns 控件中所有行的数组。只需用您想要的胶水加入数组即可:

Dim products As String = String.Join(";", txt_distributorProducts.Lines)

此外,就其价值而言,您不能像在 C# 中那样在 Visual Basic .NET 中使用转义字符串。 \n 在 Visual Basic 中等价于 Environment.NewLine.