Array problem

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 13
Reputation: MillStrike is an unknown quantity at this point 
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Array problem

 
0
  #1
May 10th, 2006
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 =)

  1. private void SongTitle()
  2. {
  3. string SelectedFileReverse;
  4. bool j = false;
  5.  
  6. //Part 1
  7. char[] strArray = SelectedFile.ToCharArray();
  8. Array.Reverse(strArray);
  9. string strReversed = new string(strArray);
  10. SelectedFileReverse = strReversed;
  11.  
  12.  
  13. int g = Convert.ToInt32(0);
  14.  
  15. //Part 2
  16. foreach (char i in SelectedFileReverse)
  17. {
  18. if (i != '\\' || j != true)
  19. {
  20. SelectedFileReversed[g] = Convert.ToString(i);
  21. }
  22. else
  23. {
  24. j = true;
  25. }
  26. g++;
  27. }
  28.  
  29. //Part 3
  30. string SelectedFileReversed2 = Convert.ToString(SelectedFileReversed);
  31. char[] strArray1 = SelectedFileReversed2.ToCharArray();
  32. Array.Reverse(strArray1);
  33. string strReversed1 = new string(strArray1);
  34. string SelectedFileTemp = strReversed;
  35.  
  36. this.lblSong.Text = SelectedFileTemp;
  37. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Array problem

 
0
  #2
May 10th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 13
Reputation: MillStrike is an unknown quantity at this point 
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Re: Array problem

 
0
  #3
May 10th, 2006
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 =/
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Array problem

 
0
  #4
May 10th, 2006
here, same result in 4 lines

  1. private void SongTitle()
  2. {
  3. char[] strArray = SelectedFile.ToCharArray();
  4. string result; = new string(strArray);
  5. string[] strArray = result.Split('\\');
  6. this.lblSong.Text = strArray[(strArray.Length - 1)]
  7. }
Last edited by campkev; May 10th, 2006 at 6:45 pm. Reason: fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 13
Reputation: MillStrike is an unknown quantity at this point 
Solved Threads: 0
MillStrike MillStrike is offline Offline
Newbie Poster

Re: Array problem

 
0
  #5
May 10th, 2006
It works perfectly! Thanks allot dude. Been haveing trouble with that piece of code all week. Big thanks for you taking your time =)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Array problem

 
0
  #6
May 10th, 2006
no problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC