954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array problem

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;
        }
MillStrike
Newbie Poster
13 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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 =/

MillStrike
Newbie Poster
13 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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)]
        }
campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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

MillStrike
Newbie Poster
13 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

no problem

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You