locked
split string based on the last special character RRS feed

  • Question

  • User-73514677 posted

    Hi.

    i have few names in my string array, which contain data as below

    abc.html

    test.asp

    03.file1.xlsx

    I am trying to split the data based on the special character period . I tried the below code, but it does not work , when it hits the third item

    char[] delimiterChars = { '.' };
    string[] filesInFolder;  // this contains the values
     //Get one name at a time)
    foreach (string fileIn in filesInFolder)
    {
    string[] words = fileIn.Split(delimiterChars);
    if(words[1].ToLower().Contains("xlsx"))) 
    {
    // do processing
    }

    How to split based on special character, when there are multiple instances of the special character?

    Thanks
     

    Tuesday, April 7, 2020 3:52 AM

Answers

  • User-73514677 posted

    Hi.

    I tried the below and it seems to work

     if(words.Last().Equals("xlsx"))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2020 5:40 AM
  • User288213138 posted

    Hi wenkatzeus,

    How to split based on special character, when there are multiple instances of the special character?

    You can also try below code:

    char[] delimiterChars = { '.' };
                string[] filesInFolder = { "abc.html", "test.asp", "03.file1.xlsx" };
    
                foreach (string fileIn in filesInFolder)
                {
                    string[] words = fileIn.Split(delimiterChars);
    
    
                    if (words[words.Length - 1] == "xlsx")
                    {
                        //do processing
               
                    }
                }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2020 2:25 AM

All replies

  • User-73514677 posted

    Hi.

    I tried the below and it seems to work

     if(words.Last().Equals("xlsx"))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2020 5:40 AM
  • User288213138 posted

    Hi wenkatzeus,

    How to split based on special character, when there are multiple instances of the special character?

    You can also try below code:

    char[] delimiterChars = { '.' };
                string[] filesInFolder = { "abc.html", "test.asp", "03.file1.xlsx" };
    
                foreach (string fileIn in filesInFolder)
                {
                    string[] words = fileIn.Split(delimiterChars);
    
    
                    if (words[words.Length - 1] == "xlsx")
                    {
                        //do processing
               
                    }
                }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 8, 2020 2:25 AM