views:

151

answers:

1

Hello

I have string like this:

G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg

How can i do to remove all text before Files (G:\Projects\TestApp\TestWeb)? The search way before files can change, so i can't count signs and remove after 20 signs.

Thanks for help :)

+7  A: 

try this

string s = oldString.Substring(oldString.IndexOf("Files"));

Thanks to comment from Meta-Knight,

if another "Files" can be in early part of the string then better would be..

string s = oldString.Substring(oldString.LastIndexOf("Files"));
Charles Bretana
Thanks, that did it :)
Frozzare
This is certainly valid, but I would like to add that you want to find a unique part of the string you want to keep (g:\uploadfiles\john\Files\Upload\ would fail). You could consider .jpg or \Files\Upload\ as well. Also, suggest .tolower() so you aren't impacted by case differences.
Mayo
I would use LastIndexOf instead. It would minimize the risk of errors.
Meta-Knight
@Frozzare, be careful with this, in that if there can be another "Files" in the beginning somewhere, you may want to reverse the code as my edited answer shows.
Charles Bretana
In addition to using LastIndexOf, it's also a good idea to search for "\Files\" instead of just "Files" in case you have a path like "c:\My really important Files\Pictures\Files\Adult Files\NSFW" for instance.
Paul McGuire