Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i remove the part "http://" from a string?

Tags:

c#

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://"?

like image 497
user1741587 Avatar asked Oct 25 '12 05:10

user1741587


People also ask

How do you delete part of a string?

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.

Is there a remove function in 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.

What is string remove?

The String Remove() method removes a specified number of characters from the string.


4 Answers

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

like image 162
Jon Skeet Avatar answered Oct 05 '22 21:10

Jon Skeet


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

like image 32
Habib Avatar answered Oct 05 '22 21:10

Habib


Try this:

if(example.StartsWith("http://"))
{
    example.substring(7); 
}
like image 27
Hhovhann Avatar answered Oct 05 '22 20:10

Hhovhann


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://"))
like image 36
Jonas W Avatar answered Oct 05 '22 22:10

Jonas W