Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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[] { "
", "
" }, StringSplitOptions.None);

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...