none
Extract Characters into different variable form a path RRS feed

  • Question

  • Hi Experts, 

    I need to extract from a file path and assign them to different variables like 

    File Path example
      filename = "C:\\Data\\Store\\Path\\SR_HDCN_Instance1_DB_Emp_FN_Client.txt"

    To identify the pattern I have the keywords SR, DB, FN embedded in the file path which tells the starting position for extraction

    output


    string SR = 'HDCN_Instance1'

    string DB = 'Emp' 

    string FN = 'Client'

    SR: Server Name, DB : Database Name, FN : FileName

    Please support

    Thanks 

    Priya

    Monday, March 9, 2020 6:42 AM

Answers

  • Hi Priya,

    Thank you for posting here.

    You can use the following code to split the path and assign it to the corresponding string.

               tring filename = "C:\\Data\\Store\\Path\\SR_HDCN_Instance1_DB_Emp_FN_Client.txt";
                string SR = null;
                string DB = null;
                string FN = null;
                string[] strs = filename.Split(new char[] { '\\' })[4].Split(new char[] { '_'});
                for (int i = 0; i < strs.Length; i++)
                {
                    if (nameof(SR) == strs[i])
                    {
                        SR = strs[i + 1]+"_"+strs[i+2];
                    }
                    if (nameof(DB) == strs[i])
                    {
                        DB = strs[i + 1];
                    }
                    if (nameof(FN) == strs[i])
                    {
                        FN = strs[i + 1];
                    }
                }
    
                Console.WriteLine(SR+"\t"+DB+"\t"+FN);

    Hope this could be helpful.

    Best Regards,

    Timon


    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.

    • Marked as answer by Priya Bange Monday, March 9, 2020 8:52 AM
    Monday, March 9, 2020 8:26 AM

All replies

  • I have literally never before seen anyone lazy enough to ask this question.

    Look at System.String Members documentation.  RTFM.  First.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Monday, March 9, 2020 6:51 AM
  • I have literally never before seen anyone lazy enough to ask this question.

    Look at System.String Members documentation.  RTFM.  First.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Thank you for the shared link. Am not a C# dev firstly so at times something may be new to someone.

    So if its lazy or not may be its a personal opinion.

    Thanks again

    Monday, March 9, 2020 6:59 AM
  • Hi Priya,

    Thank you for posting here.

    You can use the following code to split the path and assign it to the corresponding string.

               tring filename = "C:\\Data\\Store\\Path\\SR_HDCN_Instance1_DB_Emp_FN_Client.txt";
                string SR = null;
                string DB = null;
                string FN = null;
                string[] strs = filename.Split(new char[] { '\\' })[4].Split(new char[] { '_'});
                for (int i = 0; i < strs.Length; i++)
                {
                    if (nameof(SR) == strs[i])
                    {
                        SR = strs[i + 1]+"_"+strs[i+2];
                    }
                    if (nameof(DB) == strs[i])
                    {
                        DB = strs[i + 1];
                    }
                    if (nameof(FN) == strs[i])
                    {
                        FN = strs[i + 1];
                    }
                }
    
                Console.WriteLine(SR+"\t"+DB+"\t"+FN);

    Hope this could be helpful.

    Best Regards,

    Timon


    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.

    • Marked as answer by Priya Bange Monday, March 9, 2020 8:52 AM
    Monday, March 9, 2020 8:26 AM