none
strip out a file name. Remove the ext of a file string, Not the file it self RRS feed

  • Question

  • I have a string.

    string mapName = m_MapsInBase.SelectedItems[0].SubItems[1].Text;


    mapName relates to a fileName. The file name is going to have an ext. How can I use this string to stop off the exit from it and use just the name of that file. anyone know of way to do with with reading the file it self. The file in general is a *.zip file.


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Wednesday, April 10, 2019 10:51 PM

Answers

  • string mapName = Path.GetFileNameWithoutExtension(m_MapsInBase.SelectedItems[0].SubItems[1].Text);

    or if you know the file is a ".zip" file then you can do this

    string mapName = m_MapsInBase.SelectedItems[0].SubItems[1].Text.Replace(".zip", string.Empty);


    • Edited by William Xifaras Wednesday, April 10, 2019 11:38 PM
    • Marked as answer by Joesoft11a Wednesday, April 10, 2019 11:39 PM
    Wednesday, April 10, 2019 11:37 PM

All replies

  • https://www.dotnetperls.com/indexof

    https://www.c-sharpcorner.com/UploadFile/mahesh/substring-in-C-Sharp/usubs

    Use IndexOf to find the position number of the "." in the string.

    calculate the length = position-of-period - start-position of string -1  

    use substring(start-pos, lenght) 

    You should ba abl to figure it out.

    Wednesday, April 10, 2019 11:11 PM
  • string mapName = Path.GetFileNameWithoutExtension(m_MapsInBase.SelectedItems[0].SubItems[1].Text);

    or if you know the file is a ".zip" file then you can do this

    string mapName = m_MapsInBase.SelectedItems[0].SubItems[1].Text.Replace(".zip", string.Empty);


    • Edited by William Xifaras Wednesday, April 10, 2019 11:38 PM
    • Marked as answer by Joesoft11a Wednesday, April 10, 2019 11:39 PM
    Wednesday, April 10, 2019 11:37 PM
  • Thanks will. That indexof thing was throwing me off.


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Wednesday, April 10, 2019 11:39 PM
  • Thanks will. That indexof thing was throwing me off.


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software


    You better figure it out, becuase getting a string out of another string,  there is not going to be the helper function that was given to you. :)
    • Edited by DA924x Wednesday, April 10, 2019 11:53 PM
    Wednesday, April 10, 2019 11:50 PM
  • That indexof thing was throwing me off.

    A word of caution - IndexOf finds the *first* occurrence of the target in the
    subject string. Since file names can contain periods, the ext is what comes 
    after the *last* occurrence. So you should use LastIndexOf('.')

    static void Main(string[] args)
    {
        string str = "This is a sentence. And a file name..zip";
        int idx = str.LastIndexOf('.');
        if(idx == -1)
        {
            Console.WriteLine("No file extension found!");
            return;
        }
        
        //string ext = str.Substring(idx + 1); // excludes the period
        //string filename = str.Substring(0, str.Length - ext.Length - 1);
    
        string ext = str.Substring(idx); // includes the period
        string filename = str.Substring(0, str.Length - ext.Length);
    
        Console.WriteLine(str);
        Console.WriteLine(filename);
        Console.WriteLine(ext);
    
        Console.ReadLine();
    }
    
    /*
    This is a sentence. And a file name..zip
    This is a sentence. And a file name.
    .zip
    */
    

    - Wayne

    Thursday, April 11, 2019 1:45 AM