Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Remove file extension from string list

Tags:

c#

What my program does is basically it lists file names (including it's extension) from a directory into a listbox. It then has a sorting function which sorts the list strings into alphabetical order.

Lastly it has a binary search function that allows the users to input any string which the program will then compare and display the matched results into a listbox.

Now, all these functions work perfectly however I can't seem to remove the extension off of a file name after a search.

For example in the scanning and sorting part it lists the file names as: filename.mp3

Now, what I want it do when the searching button is clicked is to remove the file extension and display just the filename.

    private void buttonSearch_Click(object sender, RoutedEventArgs e)
    {

        listBox1.Items.Clear();

        string searchString = textBoxSearchPath.Text;


        int index = BinarySearch(list1, 0, list1.Count, searchString);

        for (int n = index; n < list1.Count; n++)
        {
            //Removes file extension from last decimal point ''not working''
            int i = list1[n].LastIndexOf(".");
            if (i > 0)
                list1[n].Substring(0, i);

            // Adds items to list
            if (list1[n].IndexOf(searchString, StringComparison.OrdinalIgnoreCase) != 0) break; 
            listBox1.Items.Add(list1[n]);
        }
        MessageBox.Show("Done");
    }
like image 345
ffxdean Avatar asked Dec 03 '22 23:12

ffxdean


1 Answers

C# is so easy that if something takes more than 2 minutes, there probably is a method for it in the Framework.

like image 164
nvoigt Avatar answered Dec 18 '22 05:12

nvoigt