简体   繁体   English

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

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

I am trying to parse a tab delimited text string (from a TextBox) into values. 我正在尝试将制表符分隔的文本字符串(来自TextBox)解析为值。 The idea is a user can copy and paste. 这个想法是用户可以复制和粘贴。 I've had a good search around the net and I am struggling. 我在网上进行了很好的搜索,但是我很努力。

Here is an example of the textbox string: 这是文本框字符串的示例:

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

I would like to collect all 4 values but most importantly I must collect the first two "Compressed Bright Spodumain 30". 我想收集所有4个值,但最重要的是,我必须收集前两个“ Compressed Bright Spodumain 30”。 I need to be able to add lines together as well for example if there where 2 "Compressed Bright Spodumain 30" lines it would add the number value together. 我也需要能够将行加在一起,例如,如果有2条“ Compressed Bright Spodumain 30”行将数字值加在一起。

You can split the text into rows with enter (carriage return) and then split the rows on the tab delimiter. 您可以使用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# 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