0

txtinput 中的原始文本将以这种格式显示

Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
  1. 我的目标是首先将每行拆分为两个变量,然后将它们添加到数组或列表或任何有效的方法中[行数是可变的,因此数组不起作用]

    var1[] = firstportion, var2=seconportion //到目前为止看起来像是为第一个条目工作

  2. 把它们放到变量数组/列表中[不适用于我目前拥有的循环]

  3. 使用条件 if 和正则表达式分别处理第一部分和第二部分,并将它们全部显示在第二个 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

尝试声明你的:

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

在 foreach 循环之外。

于 2013-03-19T10:09:20.020 回答
0

似乎有两个大问题。

  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 回答
0
  1. 列表应在循环外声明
  2. 你为什么使用rawInput?相反,您必须使用line进行拆分
  3. 您不需要局部变量(x,y),而是可以使用 LastOrDefault()

            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 回答