簡體   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