0

txtinput の元のテキストはこの形式で表示されます

Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
  1. 私の目標は、最初に各行を2つの変数に分割し、それらを配列またはリストなどに追加することです[行数は可変であるため、配列は機能しません]

    var1[] = firstportion, var2=seconportion //これまでのところ、最初のエントリで機能しているように見えます

  2. それらを変数配列/リストに入れます[現在持っているループでは機能しません]

  3. 最初の部分を別々に処理し、2 番目の部分を条件付き if と正規表現で別々に処理し、それらをすべて 2 番目の txtbox に表示します

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Collections;
    using System.Text.RegularExpressions;
    
    namespace Projectx
    {
    public partial class Form1 : Form
    {
            public Form1()
            {
            InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
            string rawInput = txtInput.Text;
            //string[] rawInput = txtInput.Text.Split('\n');
            int x = 1;
            int y = 1;
            foreach (string line in txtInput.Lines)
            {
                    string firstPortion= "";
                    string secondPortion = "";
                    string[] splitInput = Regex.Split(rawInput, ("\\s+"));
                    firstPortion= splitInput[0];
                    secondPortion = splitInput[1];
                    //##### This works so far ans splits each line into two variables seperated by 1 white space but not sure if it works for 2nd line and seems can't assign these new splited values into two seperate array or list with the same loop or seperate loop
                    //List<int> list = new List<int>; //(arr)
                    List<string> myList1 = new List<string>();
                    List<string> myList2 = new List<string>();
                    myList1.Add(firtPortion);
                    myList2.Add(secondPortion);
    
                    txtOutPut.Text = "modified " + myList1[x] + "  " + myList2[y]+"\r\n";
    
                    int i = 1;
                    i++;
                    x++;
                    y++;
                    }  
            }
    }
    }
    
4

3 に答える 3

0
  1. List はループの外で宣言する必要があります
  2. なぜrawInputを使用するのですか? 代わりに、ラインを使用して分割を行う必要があります
  3. 代わりに LastOrDefault() を使用できるローカル変数(x、y)は必要ありません

            List<string> myList1 = new List<string>();
            List<string> myList2 = new List<string>();
    
            string rawInput = txtInput.Text;            
    
            foreach (string line in txtInput.Lines)
            {
                string firstPortion = "";
                string secondPortion = "";
    
                string[] splitInput = Regex.Split(line, ("\\s+"));
                firstPortion = splitInput[0];
                secondPortion = splitInput[1];
    
                myList1.Add(firstPortion);
                myList2.Add(secondPortion);
    
                txtOutPut.Text = "modified " + myList1.LastOrDefault() + "  " + myList2.LastOrDefault() + "\r\n";
            }
    
于 2013-03-19T10:18:47.847 に答える
0

あなたのを宣言してみてください:

            List<string> myList1 = new List<string>();
            List<string> myList2 = new List<string>();

foreach ループの外側。

于 2013-03-19T10:09:20.020 に答える
0

2つの大きな問題があるようです。

  1. 同じリストに新しい項目を追加するのではなく、反復ごとに新しいリストのペアを作成しています。
  2. rawInput現在ではなく、各反復で変数を分割していますline

主に無関係または未使用の変数に関係する小さなものもいくつかあります。

次のようなことを試してください:

List<string> myList1 = new List<string>();
List<string> myList2 = new List<string>();
int x = 0;
foreach (string line in txtInput.Lines)
{
    string[] splitInput = Regex.Split(line, ("\\s+"));
    myList1.Add(splitInput[0]);
    myList2.Add(splitInput[1]);
    txtOutPut.Text += "modified " + myList1[x] + "  " + myList2[x] + Environment.NewLine;
    x++;
}
于 2013-03-19T10:09:50.257 に答える