remove characters after a certain character

 I'm trying to figure out to strip all characters after a certain character.  I am grabbing a file URL from a database that contains the filename, but also it's path.  I was hoping I could somehow strip out all the characters after a forward slash or backward slash.

Some of the files look like this:

c:\inetpub\wwwroot\vip2222\cases\MediaFiles\2da97d13-063b-4f69-2db42e8bbdb4\911ec365-c853-4650-9682-b464c3e17814.jpg

 MediaFiles/4353495809-04955.jpg

http://www.myhomepage.com/images/image2500010001.gif

And I just need the file name.  I tried using Replace, but that didn't work.

If anyone could offer any advice, I would appreciate it.

 

Thanks!

 

 

0
cmt9000
4/20/2009 5:25:06 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

4 Replies
553 Views

Similar Articles

[PageSpeed] 0

Do a string.Split() on the backslash character and get the last string in the array of string returned.

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx


Darrell Norton, MVP
Darrell Norton's Blog


Please mark this post as answered if it helped you!
0
DarrellNorton
4/20/2009 5:43:34 PM

 try below code

        string str10 = "http://www.myhomepage.com/images/image2500010001.gif";
        int intLastIndexof = str10.LastIndexOf('/');
        str10 = str10.Substring(intLastIndexof + 1, str10.Length - intLastIndexof - 1);


Please mark it as answer if it resolves the issue.

visit: http://technicalsol.blogspot.com
0
sumitd
4/20/2009 5:49:28 PM

 

string s = @"c:\inetpub\wwwroot\vip2222\cases\MediaFiles\2da97d13-063b-4f69-2db42e8bbdb4\911ec365-c853-4650-9682-b464c3e17814.jpg";
            char [] delimiter = new char[]{'\\'};
            int position = s.LastIndexOfAny(delimiter);
            string filename = s.Substring(position+1);
            Console.WriteLine(filename);
 
*** If this post helps you, then Mark this post as Answer ***
0
inquisitive_mind
4/20/2009 5:55:24 PM

Here is one more solution to add to what the others have provided. This handles both '/' and '\' strings.

if (str.Contains(@"\"))
str = str.Remove(0, str.LastIndexOf(@"\") + 1);
else
str = str.Remove(0, str.LastIndexOf(@"/") + 1);
  
My Blog
http://Lspence.blogspot.com

(Please MARK this post as ANSWERED, if you find it helpful)
0
lspence
4/20/2009 6:17:02 PM
Reply: