Trying to capture or group txt from a string into a list box C# -


i have .txt file containing many lines. trying capture lines start d. here trick: capture 2 proceeding lines (when d lines start) can capture site , program came from. here looking for

desired result: ![text desired][1]

also, d lines can appear multiple times in text varying length, instead of 3 lines of "d" there 1000 or 10 or it..

how go writing this, can done? anel system name: bauer node 02 program name: abu.deh1lr.pgm panel system name: bauer node 02 program name: abu.rt1lr.pgm panel system name: bauer node 02 program name: abu.rt2lr.pgm panel system name: bauer node 02 program name: abu.rt3lr.pgm panel system name: bauer node 03 program name: aba.lighting.pgm panel system name: bauer node 03 program name: aba.rt1lr.pgm panel system name: bauer node 03 program name: aba.rt2lr.pgm d 5205 loop(128 %x%sat %x%vrt %x%sas 1000 15 0 1 0 0 100 0) d 5210 dbswit(0 aba.rt2lr.hco 2.0 8.0 aba.rt2lr.ht1) d 5220 table(aba.rt2lr.vrt %x%hco 0 0 100 100) panel system name: bauer node 03 program name: aba.rt3lr.pgm panel system name: bauer node 03 program name: aba.zone.vlv.pgm panel system name: bauer node 03 program name: abu.car.plug.pgm panel system name: bauer node 04 program name: aba.rt4lr.pgm panel system name: bauer node 04 program name: bauerbush ppcl 4 panel system name: bauer node 05 program name: aba.deh1lr.pgm panel system name: bauer node 06 program name: aba.rt5lr.pgm

enter code here 

add listbox(name:mainlistbox) , button(name: gettextbutton) windows form app
, use codebehind:

using system.collections.generic; using system.io; using system.windows.forms;  namespace stackanswer {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private void gettextbutton_click(object sender, system.eventargs e)         {             var source = file.readalllines("c:\\users\\user\\desktop\\test.txt");              mainlistbox.items.clear();              list<string> result = new list<string>();             (int = 0; < source.length; i++)             {                 if (source[i].startswith("d")                     && !source[i - 1].startswith("d"))                 {                     result.add(source[i - 2]);                     result.add(source[i - 1]);                     result.add(source[i]);                 }                 else if (source[i].startswith("d"))                 {                     result.add(source[i]);                 }             }             mainlistbox.items.addrange(result.toarray());         }     } } 

wpf edition:

<window x:class="stackanswerwpf.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:stackanswerwpf" mc:ignorable="d" title="mainwindow" height="350" width="525">     <grid>         <grid.rowdefinitions>             <rowdefinition height="283*" />             <rowdefinition height="37*" />         </grid.rowdefinitions>         <listbox name="mainlistbox" />         <button name="gettextbutton" grid.row="1" click="gettext">gettext</button>     </grid> </window> 

codebehind:

using system.collections.generic; using system.io; using system.windows;  namespace stackanswerwpf {     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();         }          private void gettext(object sender, routedeventargs e)         {             var source = file.readalllines("c:\\users\\user\\desktop\\test.txt");              mainlistbox.itemssource = null;              list<string> result = new list<string>();             (int = 0; < source.length; i++)             {                 if (source[i].startswith("d")                     && !source[i - 1].startswith("d"))                 {                     result.add(source[i - 2]);                     result.add(source[i - 1]);                     result.add(source[i]);                 }                 else if (source[i].startswith("d"))                 {                     result.add(source[i]);                 }             }             mainlistbox.itemssource = result;         }     } } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -