简体   繁体   中英

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. 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". 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.

You can split the text into rows with enter (carriage return) and then split the rows on the tab delimiter.

'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>";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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