如何用C#按回车符分割字符串?

发布于 2024-08-13 01:17:27 字数 219 浏览 11 评论 0原文

我有一个 ASP.NET 页面,其中有一个名为 txbUserName 的多行文本框。然后我将 3 个名称粘贴到文本框中,它们垂直对齐:

  • Jason
  • Ammy
  • Karen

我希望能够在检测到回车符或新行时以某种方式获取这些名称并将它们拆分为单独的字符串。我认为数组可能是正确的选择。 有什么想法吗?

谢谢。

I have an ASP.NET page with a multiline textbox called txbUserName. Then I paste into the textbox 3 names and they are vertically aligned:

  • Jason
  • Ammy
  • Karen

I want to be able to somehow take the names and split them into separate strings whenever i detect the carriage return or the new line. i am thinking that an array might be the way to go.
Any ideas?

thank you.

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

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

发布评论

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

评论(7

爱殇璃 2024-08-20 01:17:27
string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

这涵盖了 \n 和 \r\n 换行符类型,并删除了用户可能输入的任何空行。

我使用以下代码进行了测试:

        string test = "PersonA\nPersonB\r\nPersonC\n";
        string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in result)
            Console.WriteLine(s);

它工作正常,分为三个字符串数组,其中包含条目“PersonA”、“PersonB”和“PersonC”。

string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

This covers both \n and \r\n newline types and removes any empty lines your users may enter.

I tested using the following code:

        string test = "PersonA\nPersonB\r\nPersonC\n";
        string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
        foreach (string s in result)
            Console.WriteLine(s);

And it works correctly, splitting into a three string array with entries "PersonA", "PersonB" and "PersonC".

空气里的味道 2024-08-20 01:17:27

将任何 \r\n 替换为 \n,然后使用 \n 进行拆分:

string[] arr = txbUserName.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());

Replace any \r\n with \n, then split using \n:

string[] arr = txbUserName.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());
巷雨优美回忆 2024-08-20 01:17:27

看一下 String.Split 函数(不确定确切的语法,我面前没有 IDE)。

string[] names = txbUserName.Text.Split(Environment.Newline);

Take a look at the String.Split function (not sure of exact syntax, no IDE in front of me).

string[] names = txbUserName.Text.Split(Environment.Newline);
俏︾媚 2024-08-20 01:17:27

String.Split

mystring.Split(new Char[] { '\n' })

String.Split?

mystring.Split(new Char[] { '\n' })
⒈起吃苦の倖褔 2024-08-20 01:17:27
using System.Text;
using System.Text.RegularExpressions;


 protected void btnAction_Click(object sender, EventArgs e)
    {
        string value = txtDetails.Text;
        char[] delimiter = new char[] { ';','[' };
        string[] parts = value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < parts.Length; i++)
        {
            txtFName.Text = parts[0].ToString();
            txtLName.Text = parts[1].ToString();
            txtAge.Text = parts[2].ToString();
            txtDob.Text = parts[3].ToString();
        }
    }
using System.Text;
using System.Text.RegularExpressions;


 protected void btnAction_Click(object sender, EventArgs e)
    {
        string value = txtDetails.Text;
        char[] delimiter = new char[] { ';','[' };
        string[] parts = value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < parts.Length; i++)
        {
            txtFName.Text = parts[0].ToString();
            txtLName.Text = parts[1].ToString();
            txtAge.Text = parts[2].ToString();
            txtDob.Text = parts[3].ToString();
        }
    }
请别遗忘我 2024-08-20 01:17:27

试试这个:

message.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

有效,如果:

var message = "test 1\r\ntest 2";

var message = "test 1\ntest 2";

var message = "test 1\rtest 2";

Try this:

message.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

Works if :

var message = "test 1\r\ntest 2";

Or

var message = "test 1\ntest 2";

Or

var message = "test 1\rtest 2";
甜心 2024-08-20 01:17:27

这取决于你想做什么。另一种选择是使用 StringReader 类并使用枚举器,这对于小型列表可能有点过分,但对于较大的字符串可能会更具内存效率:

IEnumerable<string> GetNextString(string input)
{
    using (var sr = new StringReader(input))
    {
        string s;
        while ((s = sr.ReadLine()) != null)
        {
            yield return s;
        }
    }
}

这同时支持 \n\r\n 行结尾。当它返回一个 IEnumerable 时,您可以使用 foreach 处理它,或者使用任何标准 linq 扩展(ToList()ToArray()、Where 等)。

例如,使用 foreach

var ss = "Hello\nworld\r\ntwo bags\r\nsugar";
foreach (var s in GetNextString(ss))
{
    Console.WriteLine("==> {0}", s);
}

It depends what you want to do. Another option, which is probably overkill for small lists, but may be more memory efficient for larger strings, is to use the StringReader class and use an enumerator:

IEnumerable<string> GetNextString(string input)
{
    using (var sr = new StringReader(input))
    {
        string s;
        while ((s = sr.ReadLine()) != null)
        {
            yield return s;
        }
    }
}

This supports both \n and \r\n line-endings. As it returns an IEnumerable you can process it with a foreach, or use any of the standard linq extensions (ToList(), ToArray(), Where, etc).

For example, with a foreach:

var ss = "Hello\nworld\r\ntwo bags\r\nsugar";
foreach (var s in GetNextString(ss))
{
    Console.WriteLine("==> {0}", s);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文