Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how could i remove the index from 0 to 2
C:\\Folder1\\Folder2\\Folder3\\Folder4

I want to remove this text in my string C:\\Folder1\\Folder2\\

So index[0],[1],[1]



And only display Folder3\\Folder4
Posted

If the actual folder names are unknown you may do split and join to achieve this (with a bit of LINQ)...
C#
string src = "C:\\Folder1\\Folder2\\Folder3\\Folder4";
string[ ] arr = src.Split( new char[ ] { '\\' } );
arr = arr.Where( ( value, index ) => index > 2 ).ToArray( );
string rv = string.Join( "\\", arr );
 
Share this answer
 
Comments
Kurac1 9-Feb-15 3:49am    
yes that was my solution thanks
Kornfeld Eliyahu Peter 9-Feb-15 3:50am    
You are welcome...
Kurac1 9-Feb-15 4:05am    
How could add infront my string also and in the end "\\" string rv = string.Join( "\\", arr ); this code only adds between the string but i want infront of the string also and in the the end like this \\Folder1\\Folder2\\?
Kornfeld Eliyahu Peter 9-Feb-15 4:07am    
Try this:
string.Format("\\{0}\\", string.Join( "\\", arr ));
Kurac1 9-Feb-15 4:17am    
ALright but it will add \\\\ in the end then? but i only want at index[0] \\ and not in the end at all \\?
Use Substring:
C#
string source = "C:\\Folder1\\Folder2\\Folder3\\Folder4";
string prefix = "C:\\Folder1\\Folder2\\";

string result = source.StartsWith(prefix) 
	? source.Substring(prefix.Length)
	: source;
 
Share this answer
 
Comments
Kurac1 9-Feb-15 3:31am    
But i dont now how to get out "C:\\Folder1\\Folder2\\"; from my string first?
Tomas Takac 9-Feb-15 3:33am    
I don't understand. This strips the prefix from the string. Could you be more specific?
Kurac1 9-Feb-15 3:35am    
I only have this string, string source = "C:\\Folder1\\Folder2\\Folder3\\Folder4"; and not the prefix i need first need to get out the prefix from my source string
Tomas Takac 9-Feb-15 3:36am    
And how do you know what's the prefix? What you basically want is to split the string in two. So how do you know where to split it?
Kurac1 9-Feb-15 3:38am    
as i wrote in my question using index C:\\Folder1\\Folder2\\" so C:\\ will be [0] remove that string FOlder1\\ will be [1] remove that and so an so the first 3 indexes in the string

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900