1

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

1
  • Your question is unreadable, I did understand nothing! If you need an answer, I suggest you to rewrite the question. Commented Mar 4, 2014 at 10:29

3 Answers 3

2

Try this:

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

Output:

/student/personal/contact
Sign up to request clarification or add additional context in comments.

2 Comments

What if it is "server\student\personal\contact"?
I would suggest strp.Substring(strp.IndexOf(Path.DirectorySeparatorChar))
1

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);
}

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.