I have a heck of a situation here, and i'll try to explain it.
For example I have a string containing "c:\\download\\music.mp3" and I want to make it contain only music.mp3 by "filtering" away the rest. Code might seem a bit hard to understand but heres the deal. SelectedFile contains the data I want to filter so the first part reverses the text to "3pm.cisum\\daolnwod\\:c". Second part should put every letter, until a "\" appears, in SelectedFileReversed. And third, reversing SelectedFileReversed back again to "music.mp3". Problem appears at the line " SelectedFileReversed[g] = Convert.ToString(i);" and I can't understand whats wrong. I would be very greatfull for any help =)

Notice: there is a string named SelectedFileReverse and SelectedFileReversed. Try not to mix them up =)

private void SongTitle()
        {
            string SelectedFileReverse;
            bool j = false;
            
            //Part 1
            char[] strArray = SelectedFile.ToCharArray();
            Array.Reverse(strArray);
            string strReversed = new string(strArray);
            SelectedFileReverse = strReversed;


            int g = Convert.ToInt32(0);

            //Part 2
            foreach (char i in SelectedFileReverse)
            {
                if (i != '\\' || j != true)
                {
                    SelectedFileReversed[g] = Convert.ToString(i);
                }
                else
                {
                    j = true;
                }
                g++;
            }
            
            //Part 3
            string SelectedFileReversed2 = Convert.ToString(SelectedFileReversed);
            char[] strArray1 = SelectedFileReversed2.ToCharArray();
            Array.Reverse(strArray1);
            string strReversed1 = new string(strArray1);
            string SelectedFileTemp = strReversed;

            this.lblSong.Text = SelectedFileTemp;
        }

Recommended Answers

All 5 Replies

Way too complex for the task. Also, having two such similarly-named methods is a very bad idea.

Look into the String.Split() method. It breaks a string into an array of substrings, based on any delimiter you like. Then, you simply grab the last element of the array as your filename.

Thank you for responding. I know that using simular names is very bad but it was just for the testing.
But I can't figure out how the Split function works, been searching the internet a bit. SelectedFile.Split(something); Can't get it to do what I want =/

here, same result in 4 lines

private void SongTitle()
        {
            char[] strArray = SelectedFile.ToCharArray();
            string result; = new string(strArray);
	    string[] strArray = result.Split('\\');            
            this.lblSong.Text = strArray[(strArray.Length - 1)]
        }

It works perfectly! Thanks allot dude. Been haveing trouble with that piece of code all week. Big thanks for you taking your time =)

no problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.