I have this method:
private List<string> offline(string targetDirectory)
{
    if (targetDirectory.Contains("http://"))
    {
        MessageBox.Show("true");
    }
    DirectoryInfo di = new DirectoryInfo(targetDirectory);
    List<string> directories = new List<string>();
    try
    {
        string[] dirs = Directory.GetDirectories(targetDirectory,"*.*",SearchOption.TopDirectoryOnly);
        for (int i = 0; i < dirs.Length; i++)
        {
            string t = "http://" + dirs[i];
            directories.Add(t);
        }
    }
    catch
    {
        MessageBox.Show("hgjghj");
    }
    return directories;
}
This is the part:
if (targetDirectory.Contains("http://"))
{
     MessageBox.Show("true");
}
I'm getting a directory which give me all the directories in this directory and I'm adding to each directory the string "http://".
The problem is when next time a directory is getting to the function its coming with "http://"
For example: http://c:\\ or http://c:\\windows
And then the line
 DirectoryInfo di = new DirectoryInfo(targetDirectory); // throws exception.
So I want that each time a directory is getting to the function to check if it starts with "http://" in the beginning, strip the "http://" part, get all the directories, and then add to each directory "http://" like now.
How can I remove "http://"?
You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.
C# | Remove() Method. In C#, Remove() method is a String Method. It is used for removing all the characters from the specified position of a string. If the length is not specified, then it will remove all the characters after specified position.
The String Remove() method removes a specified number of characters from the string.
I would be stricter than using Contains - I'd use StartsWith, and then Substring:
if (targetDirectory.StartsWith("http://"))
{
    targetDirectory = targetDirectory.Substring("http://".Length);
}
Or wrap it in a helper method:
public static string StripPrefix(string text, string prefix)
{
    return text.StartsWith(prefix) ? text.Substring(prefix.Length) : text;
}
It's not clear to me why you're putting the http:// as a prefix anyway though, to be honest. I can't see how you'd expect a directory name prefixed with http:// to be a valid URL. Perhaps if you could explain why you're doing it, we could suggest a better approach.
(Also, I really hope you don't have a try/catch block like that in your real code, and that normally you follow .NET naming conventions.)
The problem is how can i remove the http:// ?
You may use string.Replace, and replace the string with an empty string.
targetDirectory = targetDirectory.Replace("http://","");
or
targetDirectory = targetDirectory.Replace("http://",string.Empty);
both of them are same
Try this:
if(example.StartsWith("http://"))
{
    example.substring(7); 
}
                        You can always use the String.Replace to remove / replace characters in the string. Exampel:
targetDirectory = targetDirectory.Replace("http://", string.Empty);
And you can check if the string begins with Http:// by doing
if(targetDirectory.StartsWith("http://"))
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With