944,038 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3187
  • C# RSS
May 10th, 2006
0

Array problem

Expand Post »
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 =)

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MillStrike is offline Offline
13 posts
since Sep 2005
May 10th, 2006
0

Re: Array problem

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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 10th, 2006
0

Re: Array problem

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 =/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MillStrike is offline Offline
13 posts
since Sep 2005
May 10th, 2006
0

Re: Array problem

here, same result in 4 lines

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 10th, 2006
0

Re: Array problem

It works perfectly! Thanks allot dude. Been haveing trouble with that piece of code all week. Big thanks for you taking your time =)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MillStrike is offline Offline
13 posts
since Sep 2005
May 10th, 2006
0

Re: Array problem

no problem
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Monitoring registry additions
Next Thread in C# Forum Timeline: Pictures in RichTextBox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC