繁体   English   中英

如何将文本框字符串解析为ASP.NET VB.NET中的值

[英]How to parse a text box string into values in ASP.NET VB.NET

我正在尝试将制表符分隔的文本字符串(来自TextBox)解析为值。 这个想法是用户可以复制和粘贴。 我在网上进行了很好的搜索,但是我很努力。

这是文本框字符串的示例:

Compressed Bright Spodumain 30  Spodumain           840 m3
Compressed Crimson Arkonor  1   Arkonor         8.80 m3
Compressed Crystalline Crokite  867 Crokite         6,771.27 m3

我想收集所有4个值,但最重要的是,我必须收集前两个“ Compressed Bright Spodumain 30”。 我也需要能够将行加在一起,例如,如果有2条“ Compressed Bright Spodumain 30”行将数字值加在一起。

您可以使用enter(回车)将文本分成几行,然后在制表符分隔符上将各行分割。

'check if the textbox actually contains text
If Not String.IsNullOrEmpty(TextBox1.Text) Then

    'split the text in separate rows
    Dim rows() As String = TextBox1.Text.Split(New String() {""& vbCrLf, ""& vbLf}, StringSplitOptions.None)

    'loop all the rows
    For Each row As String In rows

        'split the row in separate items
        Dim items() As String = row.Split(vbTab)

        'loop all the individual items
        For Each item As String In items
            Label1.Text = (Label1.Text + (item.Trim + "<br>"))
        Next
    Next
End If

C#

//check if the textbox actually contains text
if (!string.IsNullOrEmpty(TextBox1.Text))
{
    //split the text in separate rows
    string[] rows = TextBox1.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

    //loop all the rows
    foreach (string row in rows)
    {
        //split the row in separate items
        string [] items = row.Split('\t');

        //loop all the individual items
        foreach (string item in items)
        {
            Label1.Text += item.Trim() + "<br>";
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM