2017-01-19 9 views
0

タブ区切りのテキスト文字列(TextBoxから)を値に解析しようとしています。アイデアは、ユーザーがコピー&ペーストできることです。私はネットの周りをうまく検索してしまい、苦労しています。ここでテキストボックス文字列をASP.NET VB.NETの値に解析する方法

は、テキストボックスの文字列の例です:

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つの値を収集したいと思いますが、最も重要なのは、私は最初の二つの「圧縮明るいSpodumain 30」を収集する必要があります。私はラインを一緒に追加することができるようにする必要があります。例えば、2つの "Compressed Bright Spodumain 30"ラインの場合は、数値を一緒に加算します。

答えて

1

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>"; 
     } 
    } 
} 
+0

それはすでに 'lines'プロパティはWinformsのではなく、Webフォームで –

+0

lines'性質を持っているので、'私はあなたが 'TextBox1.Text.Spli'を必要としないと思います。 – VDWWD

+0

私は逃した - そのWebフォーム! –

関連する問題