If you want to make sure the first character of a string is one slash, use the function:

public string PrependPath(string path, string firstCharacter)
{
    return firstCharacter + path.TrimStart(firstCharacter.ToCharArray());
}

 

To test the function use:

string firstCharacter = "/";
Console.WriteLine(PrependPath("TestPath", firstCharacter));
Console.WriteLine(PrependPath("/TestPath", firstCharacter));
Console.WriteLine(PrependPath("//TestPath", firstCharacter));
Console.WriteLine(PrependPath("///TestPath", firstCharacter));    

 

Result

/TestPath

/TestPath

/TestPath

/TestPath

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.