none
c# how to get certain part of a csv line only. RRS feed

  • Question

  • i made a program that goes though looks at a bin file and takes out note tracks based on what it finds so for example here mp7 

    program will find the following. 

    ps_wfoly_plr_sm_mpapa7_reload_empty_xmag_01

    then i need to search a csv file for the following 

    wfoly_plr_sm_mpapa7_reload_empty_xmag_01 ( or what ever my program finds 

    and i need to get the name of the sound file only witch is in this case 

    wfoly_plr_sm_mpapa7_reload_empty_xmag_lift

    wfoly_plr_sm_mpapa7_reload_empty_xmag_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_lift.LN.75.44100.all

    but the issue i have is im not sure how to do this and each csv is basically the same on layout 

    notetrack wfoly_plr_sm_mpapa7_reload_empty_xmag_01

    path reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_lift

    and the path wfoly_plr_sm_mpap7_reload_empty_xmag_lift is the only part i need to save just not sure how to accomplish this any help would be much appreciated this is a example of csv file

    wfoly_plr_sm_mpapa7_raise_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_raise.LN.75.44100.all
    wfoly_plr_sm_mpapa7_raise_quick_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_raise_quick.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_mag_release.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_mvmnt.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_05,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_055,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_06,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_035,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_mvmnt.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_05,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_bolt_release.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_06,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_035,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_bolt_release.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_05,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_mvmnt.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_045,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_05,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_bolt_release.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_06,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_fast_xmag_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_mvmnt.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_045,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_05,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_bolt_release.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_empty_xmag_06,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_empty_xmag_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_035,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_xmag_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_xmag_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_xmag_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_xmag_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_xmag_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_xmag_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_xmag_035,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_xmag_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_fast_xmag_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_fast_xmag_end.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_xmag_01,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_xmag_lift.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_xmag_02,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_xmag_magout.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_xmag_03,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_xmag_mvmnt.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_xmag_04,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_xmag_magin_v2_01.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_xmag_045,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_xmag_magin_v2_02.LN.75.44100.all
    wfoly_plr_sm_mpapa7_reload_xmag_05,,reloads\iw8_mpapa7\wfoly_plr_sm_mpapa7_reload_xmag_end.LN.75.44100.all
    

    thank you in advance 

    Tuesday, May 19, 2020 2:45 PM

All replies

  • Something to the tune of this.

    using System;
    using System.IO;
    using System.Linq;
    
    namespace ConsoleApp1
    {
        public class Demo
        {
            public static void Main(string[] args)
            {
                /*
                 * Replace with your file
                 */
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt");
                var data = File.ReadAllLines(fileName)
                    .Select((line, index) => new {Line = line, RowIndex = index +1});
    
                var searchFor = "wfoly_plr_sm_mpapa7_reload_empty_xmag_01";
    
                var result = data.FirstOrDefault(item => item.Line.Contains(searchFor));
                if (result != null)
                {
                    Console.WriteLine($"Found on line {result.RowIndex}");
                    Console.WriteLine(string.Join("\n", result.Line.Split(',')));
                }
                else
                {
                    Console.WriteLine("Not found");
                }
    
                Console.ReadLine();
            }
        }
    } 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 19, 2020 3:19 PM
    Moderator
  • Something to the tune of this.

    using System;
    using System.IO;
    using System.Linq;
    
    namespace ConsoleApp1
    {
        public class Demo
        {
            public static void Main(string[] args)
            {
                /*
                 * Replace with your file
                 */
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt");
                var data = File.ReadAllLines(fileName)
                    .Select((line, index) => new {Line = line, RowIndex = index +1});
    
                var searchFor = "wfoly_plr_sm_mpapa7_reload_empty_xmag_01";
    
                var result = data.FirstOrDefault(item => item.Line.Contains(searchFor));
                if (result != null)
                {
                    Console.WriteLine($"Found on line {result.RowIndex}");
                    Console.WriteLine(string.Join("\n", result.Line.Split(',')));
                }
                else
                {
                    Console.WriteLine("Not found");
                }
    
                Console.ReadLine();
            }
        }
    } 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    that finds the line perfectly :) thank you but how would i go about getting just the value form there 

    wfoly_plr_sm_mpapa7_reload_empty_xmag_lift

    each number has a diffent sound attached to it im trying to get the line and getting 

    the value after 

    reloads\iw8_mpapa7\

    and want to get that value form the searched value so search works perfectly fines the line but some how need to parse it to just give me the sound name in this case its 

    wfoly_plr_sm_mpapa7_reload_empty_xmag_lift

    just not sure how to get those values form it as this the sound name i need to take note of as well.

    thank you for your quick response and very much appropriated 

    Tuesday, May 19, 2020 3:53 PM
  • Hi elfenliedtopfan55,
    First, you can use File.ReadAllLines method to read all lines of the file into a string array.
    And traverse the array to determine whether it contains "wfoly_plr_sm_mpapa7_reload_empty_xmag_01". Then you will get a whole row of data.
    Next you can use String.Substring method to intercept the substring you want.
    I viewed all lines in your csv file and found that a certain part you want comes after the second character"\" and before the first character"."
    You can use String.IndexOf and String.LastIndexOf method to get these indexes
    Here is a code example you can refer to.

    static void Main(string[] args)
    {
        string csvFile = @"C:\Users\Desktop\mp7.csv";
        string[] lines = File.ReadAllLines(csvFile);
               
        foreach (var s in lines)
        {
            if (s.Contains("wfoly_plr_sm_mpapa7_reload_empty_xmag_01")) 
            {   
                int lastindex = s.IndexOf(".");
                // get second character"\" index
                int startindex = s.LastIndexOf("\\");
                string p = s.Substring(startindex+1,lastindex-startindex-1);
                Console.WriteLine(p);
            } 
        }
    }

    The result:


    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 20, 2020 6:54 AM