Shanu
Shanu

Reputation: 21

Get Substring directory path not include first folder name in substring

Basically I want to sub-string directory path for example path is "server/student/personal/contact" I want to path like "/student/personal/contact". That's mean first folder name I don't want to in in path. Every time this path is change by project requirement so how to remove first folder name from string path.

problem that here in string path first Folder name not same name every time So please help for this how to remove first folder name from string path

Upvotes: 1

Views: 3964

Answers (3)

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

you can simply do this :

string path = "server/student/personal/contact";
//IndexOf() gives you the first occurrence of the character.
int firstSlash=path.IndexOf('/');
string modifiedPath = path.Substring(firstSlash);

Upvotes: 0

Andrew
Andrew

Reputation: 83

I usually write something like this

string example = "server/student/personal/contact";
var paths = example.Split('/').ToList();
if (paths.Any())
{
    paths.RemoveAt(0);
}
string result = string.Join("/", paths);

or you can:

string example = "server/student/personal/contact";
var pos = example.IndexOf("/", System.StringComparison.Ordinal);
if (pos > 0)
{
    example = example.Substring(pos);
}

Upvotes: 1

Sadique
Sadique

Reputation: 22841

Try this:

string strp = "server/student/personal/contact";
strp = strp.Substring(strp.IndexOf("/"));

Output:

/student/personal/contact

Upvotes: 2

Related Questions