如何控制添加数组项的哪一行到 [英] How Can I Control Which Line I Add Items Of An Array To

查看:49
本文介绍了如何控制添加数组项的哪一行到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件打印到5个组中的列表框。它是3个字母的组合,我需要列表框来阅读;

001行:ABC DEF GHI JKL MNO

第002行:PQR STU VWX YZA



除了明显用文件中的组合替换字母,因为文件可以切换数字线条不明确,需要连续计数,但总是保持3位数。请帮忙。谢谢



到目前为止,我有类似的东西,但是如果它是正确的idk



I need to print a file to a listbox in groups of 5. Its a combination of 3 letters and i need the listbox to read;
"Line 001: ABC DEF GHI JKL MNO
Line 002: PQR STU VWX YZA"

except replacing the letters with the combinations in the file obviously and since the file can be switched the number of lines is not definite and needs to count up continuously but always hold 3 digits. Please help. Thanks

So far i have something like this but idk if its correct

public partial class frmDNA : Form
   {
       int x = 1;
       String Path;
       StreamReader DNAFile = new StreamReader("/documents/visual studio 2013/Projects/DNA/DNA.txt");
       int Index;
       int Lines = File.ReadLines("/documents/visual studio 2013/Projects/DNA/DNA.txt").Count();
       public frmDNA()
       {
           InitializeComponent();
       }
      private void btnPrint_Click(object sender, EventArgs e)
       {
           String DNA = "Line " + x.ToString("000") + " = ";
           string[] DNAarray = new string[Lines];
           int Begin, end;
           Begin = 0;
           end = 5;
           while (Index < DNAarray.Length && !DNAFile.EndOfStream)
           {
               DNAarray[Index] = DNAFile.ReadLine();
               while (Begin < end)
               {
                   DNA  += DNAFile.ReadLine() + " ";
                   Begin++;
                   x++;
               }
               end += 5;
               Index++;
               lstOut.Items.Add(DNA);
               Begin += 5;
           }



       }

       private void frmDNA_Load(object sender, EventArgs e)
       {

       }
   }







But

lstOut.Items.Add(DNA);

显示错误的说DNA未分配。

is showing up as wrong saying DNA is unassigned.

推荐答案



First ,谷歌c#读取文本文件行。请参阅MSDN示例。


First, Google "c# read line of text file". Refer to the MSDN example.



我更喜欢使用使用语句而不是打开和关闭文件。替换示例的的主体,而语句包含您需要在线执行的任何编辑,然后将行添加到ListBox。


I prefer to use a using statement rather than opening and closing a file. Replace the body of the example's while statement with whatever editing you need to perform on line and then add line to the ListBox.



以下示例假定System.IO和System.Text出现在using指令中。将c:\\gustafson \\ test.txt替换为您的文本文件名称


The following example assumes that System.IO and System.Text appear in a using directive. Replace c:\\gustafson\\test.txt with the name of your text file

public void read_file_into_listbox ( )
    {
    StringBuilder   concatenated = new StringBuilder ( );
    int             item_count = 0;
    string          line;
    int             line_count = 0;

    list_box.BeginUpdate ( );
    using ( StreamReader file =
                         new StreamReader (
                            "c:\\gustafson\\test.txt" ) )
        {
        concatenated.Append ( "Line " );
        while ( ( line = file.ReadLine ( ) ) != null )
            {
            item_count++;
            if ( item_count == 1 )
                {
                line_count++;
                concatenated.AppendFormat (
                    "{0:d3}: {1} ",
                    line_count,
                    line.ToUpper ( ).Trim ( ) );
                }
            else if ( item_count == 5 )
                {
                concatenated.AppendFormat (
                    "{0}",
                    line.ToUpper ( ).Trim ( ) );
                list_box.Items.Add (
                    concatenated.ToString ( ) );
                concatenated.Length = 0;
                concatenated.Append ( "Line " );
                item_count = 0;
                }
            else
                {
                concatenated.AppendFormat (
                    "{0} ",
                    line.ToUpper ( ).Trim ( ) );
                }
            }
        if ( item_count > 0 )
            {
            list_box.Items.Add ( concatenated.ToString ( ) );
            }
        }
    list_box.EndUpdate ( );
    }
}



尽管使用+运算符可以进行字符串连接,使用StringBuilder可以获得更高效的字符串操作。


Although string concatenation can occur by using the + operator, more efficient string manipulation can be had by using StringBuilder.



执行此代码时,编辑文件的行将出现在列表框中。


When this code executes, the edited lines of the file will appear in the listbox.



希望有所帮助。


Hope that helps.


这篇关于如何控制添加数组项的哪一行到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆