在文本框中每行的开头和结尾添加新字符

发布于 2024-12-08 21:07:39 字数 153 浏览 0 评论 0原文

我有一个启用了多行的文本框,并且想要在开头和结尾添加一个字符串 每一行,因此每一行都会更改为

a + line + b

Now I Know it has to do with a foreach 循环,但不知道如何写出来。

I have a textbox with multiline enabled, and want to add a string at the beginning and end of
each line, so every line would be changed to

a + line + b

Now I know it has to do with a foreach loop, but don't know how to write it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开柳相依 2024-12-15 21:07:39

好吧, 属性可能就是您想要的。三个选项:

string[] lines = textBox.Lines;
for (int i = 0; i < lines.Length; i++)
{
     lines[i] = a + lines[i] + b;
}
textBox.Lines = lines;

或:

textBox.Lines = Array.ConvertAll(textBox.Lines, line => a + line + b);

或:

textBox.Lines = textBox.Lines
                       .Select(line => a + line + b)
                       .ToArray();

Well, the Lines property is probably the one you want. Three options:

string[] lines = textBox.Lines;
for (int i = 0; i < lines.Length; i++)
{
     lines[i] = a + lines[i] + b;
}
textBox.Lines = lines;

Or:

textBox.Lines = Array.ConvertAll(textBox.Lines, line => a + line + b);

Or:

textBox.Lines = textBox.Lines
                       .Select(line => a + line + b)
                       .ToArray();
一身软味 2024-12-15 21:07:39

您可以对整个文本使用替换:

text = a + text.Replace(Environment.NewLine, b + Environment.NewLine + a) + b;

You can use a replace on the entire text:

text = a + text.Replace(Environment.NewLine, b + Environment.NewLine + a) + b;
红衣飘飘貌似仙 2024-12-15 21:07:39

既然你提到了 foreach,这里还有另一种方法。

var newLines = new List<string>(textBox1.Lines.Length);

foreach (string line in textBox1.Lines)
   newLines.Add(a + line + b);

textBox1.Lines = newLines.ToArray();

Since you mentioned foreach, here's another way.

var newLines = new List<string>(textBox1.Lines.Length);

foreach (string line in textBox1.Lines)
   newLines.Add(a + line + b);

textBox1.Lines = newLines.ToArray();
水水月牙 2024-12-15 21:07:39

这是我用来将字符 a 和 b 添加到包含一堆行的字符串的开头和结尾的方法:

     public string Script;
     string[] lines = Script.Split(new[] { '\r', '\n' });
                    for (int i = 0; i < lines.Length; i++)
                    {
                        lines[i] = a + lines[i] + b;
                        if (!lines[i].Equals("\"\"+"))
                        {
                            Console.WriteLine(lines[i]);
                            Result += lines[i]+"\n";
                        }
                    }

Here is what i use to add the characters a and b to the beginning and the end to a string that contains a bunch of lines :

     public string Script;
     string[] lines = Script.Split(new[] { '\r', '\n' });
                    for (int i = 0; i < lines.Length; i++)
                    {
                        lines[i] = a + lines[i] + b;
                        if (!lines[i].Equals("\"\"+"))
                        {
                            Console.WriteLine(lines[i]);
                            Result += lines[i]+"\n";
                        }
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文